누군가이 코드가 왜 데이터 응답을 나타내지 않는지 내게 말할 수 있습니까? 이것은 각각 의 간단한 예이며 각각 기능입니다.각 함수를 사용하여 AJAX html 응답 수정
<html>
<div class="new">
<div class="span4">
<h2>Heading1</h2>
<p>Text1</p>
<p><a href="#">Link1</a></p>
</div>
<div class="span4">
<h2>Heading2</h2>
<p>Text2</p>
<p><a href="#">Link2</a></p>
</div>
</div>
</html>
여기에 주요 파일 몸 :
<!DOCTYPE html>
<body>
<textarea id="gethtml" rows="40" cols="125"></textarea>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
"use strict";
$(function() {
$.get('file.html', function(data) {
ajaxfunction(data);
});
function ajaxfunction(data) {
$(data).find(".span4").each(function() {
$(this).find("p").eq(0).text("Hello");
$(this).find("p").eq(0).after("<p>newline</p>");
console.log($(this));
});
$("#gethtml").val(data);
}
});
</script>
</body>
</html>
당신은 콘솔이 각 span4에 대한 정확한 데이터를 표시하는 것을 볼 수 있지만 왜 수 있습니다 여기에
는 아약스가 HTML 파일을 요청 변경 사항을 반영하기 위해 "데이터"문자열이 업데이트되지 않습니까? 텍스트 영역에서편집
function ajaxfunction(data) {
var tree = $("<div>" + data + "</div>");
$(tree).find(".span4").each(function() {
$(this).find("p").eq(0).text("Hello");
$(this).find("p").eq(0).after("<p>newline</p>");
console.log($(this));
});
$("#gethtml").val(tree.html());
}
이 곳, 즉 <p>Hello</p><p>newline</p>
. 그래서 새 줄을 추가하려면 문자열로 수정해야합니다. 맞습니까?
감사합니다.
'tree'는 이미 jQuery 객체입니다. '$()'를 호출하지 마십시오. – SLaks