admin管理员组文章数量:1341409
Why doesn't this work in jQuery 1.4.2?
var $list = $([]);
for(var i=0; i<50; i++) {
$list.add( $('<div/>', { id: 'jake', class: 'test' }).data('test', { hi: 'hello' }) );
}
alert($list.size()); // 0
Thanks!
Why doesn't this work in jQuery 1.4.2?
var $list = $([]);
for(var i=0; i<50; i++) {
$list.add( $('<div/>', { id: 'jake', class: 'test' }).data('test', { hi: 'hello' }) );
}
alert($list.size()); // 0
Thanks!
Share asked May 14, 2010 at 5:29 tabertaber 3,2404 gold badges47 silver badges72 bronze badges 1- Correct explanation given at : [jQuery add elements to empty selection?][1] [1]: stackoverflow./questions/7533929/… – mohanrajt Commented Apr 18, 2013 at 7:00
2 Answers
Reset to default 11Pointing back the reference list again works for me; e.g. $list = $list.add( $('<div/>') );
var $list = $([]);
for(var i=0; i<50; i++) {
$list=$list.add( $('<div/>', { 'id': 'jake'+i, 'class': 'test' }).data('test', { hi: 'hello' }) );
}
alert($list.size()); // 50
Why add
doesn't work I don't know, but you can replace it with push
due to jQuery being an Array-like object, which should do what you want.
本文标签: javascriptadding newly created dom elements to an empty jQuery objectStack Overflow
版权声明:本文标题:javascript - adding newly created dom elements to an empty jQuery object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743671394a2519594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论