그래서 AJAX 요청에서 얻은 다양한 다른 HTML을로드하는 div (id = "content")가 있습니다. 성공적으로로드되었는지 알 수 있지만로드 이벤트가 append ()와 함께 작동하지 않는 것 같습니다.
상황을 설명하기 위해이 간단한 예제가 있습니다 (실제 경우 'load'이벤트에 대한 리스너는 스크립트 섹션에로드되는 다른 파일에 등록됩니다)
<html>
<head>
<script
type="text/javascript"
src="http://code.jquery.com/jquery-latest.min.js">
</script>
</head>
<body>
<div id="content"><div>
<script type="text/javascript">
$(document).ready( function () {
// this does not seem to work
$("#myDiv").on('load', function () {
console.log("myDiv loaded");
});
// neither does this
$("#content").on('load', "#myDiv", function () {
console.log("myDiv loaded by delegation");
});
// the content to append will come from the network
$("#content").append($("<div id='myDiv'>myDiv</div>"));
});
</script>
</body>
</html>
물론 실제 경우에는 상황이 더 복잡하므로 (로드 된 데이터는 무엇이든 가능) 유효한 옵션이 아닙니다.
Ajax 요청에서 데이터를 얻는 경우 실제로 Ajax done () 함수를 사용해야합니다. 호출이 비동기이기 때문에 div의 변경 사항을들을 수 없습니다. 또 다른 해결책은 div에 본문 델리게이트를 추가하고 변경 이벤트를 수신하도록하는 것입니다.
$("body").delegate("mydividentifier", "change", function(f){});
$.ajax().done(function(f){});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다