开发者

jQuery: How to create element then wrap this around another existing element?

So I know how to use .wrap, .wrapInner and .wrapAll but I am wondering how to use the quick creation syntax introduced in jQuery 1.4 and the wrap function together.

Basically I want to be able to use

var targetUl = $(this), // would be populated by script
    maxWidth = 1400;    // would be populated by script

$('<div />', {
    'id':'wrap',
    'css': {
        'width': maxWidth,
        'overflow':'hidden'
开发者_高级运维    }
}).wrapAround(targetUl);

Kinda like the .appendTo method works but for wrapping stuff…

Can this be done?

Thanks.


targetUl.wrap(
    $('<div />', {
        'id':'wrap',
        'css': {
            'width': maxWidth,
            'overflow':'hidden'
        }
    })
);


jQuery .wrap() is what you're looking for. It provides this functionality in a neater way

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜