问题描述
我正在使用 jQuery.append()动态添加一些元素.有没有办法获取这些新插入的元素的jQuery集合或数组?
I'm using jQuery.append() to add some elements dynamically. Is there any way to get a jQuery collection or array of these newly inserted elements?
所以我要这样做:
$("#myDiv").append(newHtml);
var newElementsAppended = // answer to the question I'm asking
newElementsAppended.effects("highlight", {}, 2000);
推荐答案
有一种更简单的方法可以做到这一点:
There's a simpler way to do this:
$(newHtml).appendTo('#myDiv').effects(...);
首先使用 jQuery(html [, ownerDocument ])
创建newHtml
,从而扭转了局面使用 appendTo(target)
(请注意"To
"位)将其添加到#mydiv
.
This turns things around by first creating newHtml
with jQuery(html [, ownerDocument ])
, and then using appendTo(target)
(note the "To
" bit) to add that it to the end of #mydiv
.
因为您现在 start 以$(newHtml)
开始,所以appendTo('#myDiv')
的最终结果是html的新位,而.effects(...)
调用将在该 new 上.一点点的html.
Because you now start with $(newHtml)
the end result of appendTo('#myDiv')
is that new bit of html, and the .effects(...)
call will be on that new bit of html too.
这篇关于jQuery append()-返回附加的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!