2014-12-06 3 views
3

ajax를 통해로드 된 내용을 수정해야하지만 액세스 할 수 없습니다. 초기로드를 수행하는 js 파일에 액세스 할 수 없으므로 별도의 함수를 작성하여 컨텐츠를 변경해야합니다.jQuery를 사용하여 ajax를 통해로드 된 요소에 액세스 할 수 없습니다.

사용자가 페이지에서 아무 것도 클릭하지 않거나 상호 작용하지 않고 콘텐츠를 자동으로 수정해야합니다.

아래 예제에서 'news'div는 하드 코딩되고 'article'div는 ajax를 통해로드됩니다.

는 HTML

<div id="news"> 
<div class="article"><img src="headline1_small.jpg"> 
<div class="article"><img src="headline2_small.jpg"> 
<div class="article"><img src="headline3_small.jpg"> 
</div> 

jQuery를

$(document).ready(function() { 
    console.log($("#news").html()); 
}); 

콘솔

<div id="news"> 
</div> 

어떻게 기사를 액세스 할 수 있습니까? img src에서 '_small'을 제거하고 싶습니다.

감사합니다.

+0

난 당신이 사용자 상호 작용에 의존 아직 동적으로 수정 DOM 객체를 추가하지 않으려면 당신이 운이 것 같아요. 다음과 같은 DOM 변이 이벤트를들을 수 있습니다. https://developer.mozilla.org/en/docs/Web/API/MutationObserver – Terry

+0

아래의 ajaxStop의 답변이 잘 작동하는 것 같습니다. – DLL

답변

2

이 작동합니다 :

$(document).ajaxStop(function() { 
    $("#news").find('img').attr("src", function(_, src){ 
     return src.replace('_small', ''); 
    }); 
}); 
+0

감사! 완벽하게 작동합니다! – DLL

0

가 어떻게 기사를 액세스 할 수 있습니까? img src에서 '_small'을 제거하고 싶습니다. 이 같은 예를 들어

: 당신은 당신이 div의이 AJAX를 통해로드 된 문서를했다

$(document).ajaxSuccess(function() { 
    // above code 
}); 
+0

이유는 모르겠지만 ajaxStop은 ajaxSuccess가 필요한 것보다 더 잘 작동합니다. – DLL

0

AJAX 성공적인 완료에이 코드를 실행할 수 있습니다

$('#news .article img').attr('src', function() { 
    return this.src.replace('_small', ''); 
}); 

,이 스크립트는 것 같다 로드되기 전에 실행 중입니다. #news 뒤에이 스크립트를 넣으면 실행될 때 모든 내용이 이미로드됩니다.

<div id="news"> 
<div class="article"><img src="headline1_small.jpg"> 
<div class="article"><img src="headline2_small.jpg"> 
<div class="article"><img src="headline3_small.jpg"> 
</div> 

<script> 
$(document).ready(function() { 
    console.log($("#news").html()); 
}); 
</script> 
+0

파일 끝까지 코드를 옮겨 보았지만 아무 효과가 없었습니다. ajaxStop 함수가 가장 잘 작동했습니다. – DLL

0

img 태그의 src에서 "_small"을 제거하려면 다른 방법이 있습니다. #news div의 html() -> innerHTML에 액세스 할 필요가 없습니다.

모든 ".article"div를 잡고 img 태그를 변경하면됩니다.

$(".article img").each(function() { 
    $(this).attr('src', $(this).attr('src').replace("_small", "")); 
}); 

덕분에

관련 문제