默認jQuery的追加只支持.append(content [, content ])
,如何實現下面的代碼:如何與內容和afterAppending功能附加在jQuery的
$(body).append("<p>prpr</p>", function() {
// do smth after append
})
默認jQuery的追加只支持.append(content [, content ])
,如何實現下面的代碼:如何與內容和afterAppending功能附加在jQuery的
$(body).append("<p>prpr</p>", function() {
// do smth after append
})
不認爲你需要一個回調,.append()
DOM操作是同步的。所以只需將函數放在.append()
行下面
在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>
$("div").each(
function(){
$(this).append("<p>prpr</p>");
function() {
// do smth after append
}
}
}
基本上您請求的回調? – Mouser
看看這個http://stackoverflow.com/questions/6068955/jquery-function-after-append –