开发者

adding newly created dom elements to an empty jQuery object

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!


Pointing 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜