Javascript jQuery将样式复制到另一个元素
我的问题如下: 我有如下HTML代码:Javascript jQuery将样式复制到另一个元素,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我的问题如下: 我有如下HTML代码: <div class="rsContent"> <div class="rsImg" style="width:900px;height:900px; margin-left:-450px; margin-top: -450px;"></div> <div class="rsABBlock"></div> <div class="rsABBlock"></div>
<div class="rsContent">
<div class="rsImg" style="width:900px;height:900px; margin-left:-450px; margin-top: -450px;"></div>
<div class="rsABBlock"></div>
<div class="rsABBlock"></div>
</div>
<div class="rsContent">
<div class="rsImg" style="width:1200px;height:1200px; margin-left:-600px; margin-top: -600px;"></div>
<div class="rsABBlock"></div>
<div class="rsABBlock"></div>
<div class="rsABBlock"></div>
</div>
我想要的代码只需要执行一次。
这就是我试过的
jQuery(".rsContent").each(function(){
var style_var = jQuery(".rsImg").attr("style");
jQuery(".rsABBlock").each(function(){
jQuery(this).attr("style",style_var);
});
});
试试这个
jQuery(".rsContent").each(function(){
var style_var = jQuery(this).find(".rsImg").attr("style");
jQuery(this).find(".rsABBlock").each(function(){
jQuery(this).attr("style",style_var);
});
})
您必须添加此<代码>jQuery(this)。查找您可以用更少的代码完成此操作
jQuery(".rsImg").each(function(){
jQuery(this).siblings().attr('style', jQuery(this).attr("style"));
// or if there are other siblings and you need to specify rsABBlock
// jQuery(this).siblings(".rsABBlock").attr('style', jQuery(this).attr("style"));
})
jQuery(".rsContent").each(function(){
var style_var = jQuery(this).find(".rsImg").attr("style");
jQuery(this).find(".rsABBlock").each(function(){
jQuery(this).attr("style",style_var);
});
})
但是要调试原始代码,您在.find(“.rsImg”)
中缺少了一个句点,您需要做的就是使用jQuery(this.find()
工作演示:您的内部“.rsABBlock”应该从您正在创建的当前“.rsImg”开始
jQuery("div.rsContent > div.rsImg").each(function () { //Get DIV with class .rsImg which is the child of a DIV with class .rsContent
var style_var = jQuery(this).attr("style");
jQuery(this).siblings("div.rsABBlock").each(function () { //Iterate through the siblings, not all .rsABBlock.
jQuery(this).attr("style", style_var);
});
});
如果您只想替换样式,则不需要在内部循环中使用“.each”。只需使用“.attr”。它将为所有选定的元素调用
jQuery("div.rsContent > div.rsImg").each(function () { //Get DIV with class .rsImg which is the child of a DIV with class .rsContent
var style_var = jQuery(this).attr("style");
jQuery(this).siblings("div.rsABBlock").attr("style", style_var);
});
你能展示一下你试过的jQuery吗?我想这样做可以…:
jQuery(.rsContent”).each(function(){var style_var=jQuery(“rsImg”).attr(“style”);jQuery(.rsABBlock”).each(function(){jQuery(this.attr(“style”,style_var);}
你在find(.rsImg”)中缺少一个句点
您所需要做的就是使用jQuery(this.find())将每个
循环中的元素作为目标
.rynhe几乎给了你答案,但和你一样错过了一段时间。工作版你能纠正他并作为答案发布…因为我真的看不出他遗漏了什么…他的代码破坏了下面的所有代码…当然,我发布了一个修改过的答案和一个更短的解决方案。你错过了一段时间.find(“.rsImg”)
。这是一个工作版本,我知道它应该可以工作……但不知怎么的,它破坏了我的代码……你想看网站吗?NP.OP错过了
,他已经更新了这个问题。-使你的jQuery选择更加严格,以避免将来出现意外问题。只需使用jQuery(.rsABBlock))将选择所有其他元素,即使它不在目标DIV(“.rsContent”)-关于@rynhe的解决方案,如果您不循环通过“.rsContent”,而是通过“.rsImg”,它将起作用。
jQuery("div.rsContent > div.rsImg").each(function () { //Get DIV with class .rsImg which is the child of a DIV with class .rsContent
var style_var = jQuery(this).attr("style");
jQuery(this).siblings("div.rsABBlock").attr("style", style_var);
});