2017-01-04 48 views

回答

2

不認爲你需要一個回調,.append() DOM操作是同步的。所以只需將函數放在.append()行下面

0

在jQuery中,您可以檢查$(element).length以確保新的DOM節點已被添加。

工作實例:

$(document).ready(function(){ 
 
    
 
    $('body').append('<p class="newParagraph1">This is a new paragraph, added via a script.</p>'); 
 
    
 
    $('body').append('<p class="newParagraph2">This is a second new paragraph, added via a script.</p>'); 
 

 
    if ($('.newParagraph1').length) { 
 
console.log('The first dynamically added paragraph just loaded.'); 
 
    } 
 
    
 
    if ($('.newParagraph2').length) { 
 
console.log('The second dynamically added paragraph just loaded.'); 
 
    } 
 
    
 
    if ($('.newParagraph3').length) { 
 
console.log('The third dynamically added paragraph just loaded.'); 
 
    } 
 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<p>This paragraph is already in the markup</p>

0
$("div").each(
    function(){ 
     $(this).append("<p>prpr</p>"); 
     function() { 
       // do smth after append 
     } 
    } 
}