jQuery: How to create element then wrap this aroun

2020-04-12 08:43发布

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.

2条回答
姐就是有狂的资本
2楼-- · 2020-04-12 08:51

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

查看更多
狗以群分
3楼-- · 2020-04-12 08:55
targetUl.wrap(
    $('<div />', {
        'id':'wrap',
        'css': {
            'width': maxWidth,
            'overflow':'hidden'
        }
    })
);
查看更多
登录 后发表回答