FFi*_*ish 3 html css jquery clone
在我克隆 DIV 并可以更改其 CSS 属性后,但找不到如何更改其子 DIV 的 CSS。在下面的示例中,我想将 #details-child 的背景从黄色更改为蓝色。CSS:
#wrapper {
width:200px;
background:gray;
}
#details {
width:200px;
height:100px;
background:green;
}
#details-child {
width:100px;
height:100px;
background:yellow;
}
Run Code Online (Sandbox Code Playgroud)
JS:
jQuery.noConflict();
jQuery(document).ready(function() {
// clone
clone_details = jQuery("#details").clone();
// change CSS
clone_details.css({'margin-top':'50px', 'background':'red'});
// jQuery("#details-child").css('background','blue'); // changes original div
// render clone
jQuery("#wrapper").append(clone_details);
});
Run Code Online (Sandbox Code Playgroud)
一旦你有了一个 jquery 对象,你就可以在该对象的范围内进行选择器搜索$.find()
clone_details.find('#details-child').something();
但是,您不希望最终到处都有重复的 ID,因此我建议您改用类而不是 ID,因为它们必须是唯一的。