默认jquery仅附加支持.append( content [, content ] )
,如何实现以下代码:
$(body).append("<p>prpr</p>", function() {
// do smth after append
})
答案 0 :(得分:2)
不要认为你需要回调,.append()
DOM操作是同步的。所以只需将函数放在.append()
行
答案 1 :(得分: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.');
}
})
&#13;
<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>
&#13;
答案 2 :(得分:0)
$("div").each(
function(){
$(this).append("<p>prpr</p>");
function() {
// do smth after append
}
}
}