开发者

create element on the fly and append a existing element to this, with using jquery

开发者 https://www.devze.com 2023-01-22 20:59 出处:网络
my existingcode as follows $(\'#\'+ContainerId).find(\'img\').each(function(){ // ... }); I want to crate a div element on the fly and append this existing img to this new created div element.

my existing code as follows

$('#'+ContainerId).find('img').each(function(){
    // ...
});

I want to crate a div element on the fly and append this existing img to this new created div element. Res开发者_C百科ult must be like this which I want

<div style="text-align:center"><img src=".." /></div>


$('<div/>', {
    css:   {
        'text-align':   'center'
    }
}).append($('#'+ContainerId).find('img'));

Ref.: $(), .append()


You can use wrap(), and you don't even need each():

$('#'+ContainerId).find('img').wrap('<div style="text-align:center"></div>');

Example: http://jsfiddle.net/AndyE/WyXFb/

0

精彩评论

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