[英]Copy value from one element to another
I have two pairs of elements. 我有两对元素。 First pair is
.original
, which contains child elements with each having specific font-size. 第一对是
.original
,其中包含每个具有特定字体大小的子元素。 The second is .copy
with no font-size defined within it's child elements. 第二个是
.copy
,在其子元素中未定义字体大小。
When a user clicks one of the .original
elements it should copy the css font-size
from it's children to the children of the .copy
element with the same index. 用户单击
.original
元素之一时,应将css font-size
从其子元素复制到具有相同索引的.copy
元素的.copy
元素。 For example, if a user clicks first .original
element the font-size value should be copyied to children of the first .copy
element. 例如,如果用户单击第一个
.original
元素,则应该将font-size值复制到第一个.copy
元素的子级中。
Currently i can only copy the last font-size value with the loop below. 目前,我只能使用下面的循环复制最后一个字体大小的值。
$('.original').click(function() { $(this).find('div').each(function(i) { font = $(this).css('font-size'); len = $(this).length; for (var i = 0; i < len; i++) { $('.copy div').css('font-size', font) } }) })
div { display: inline-block; width: 100%; } div > div { display: inline-block; content: ""; width: 30px; height: 30px; float: left; } .original { background: #bbb; cursor: pointer; } .original > div { padding-right: 10px; } .copy { width: 100%; display: block; margin-top: 20px; clear: both; } .copy > div { font-size: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="t-1 original"> <div style="font-size: 14px">Test</div> <div style="font-size: 16px">Test</div> <div style="font-size: 18px">Test</div> </div> <div class="t-2 original"> <div style="font-size: 20px">Test</div> <div style="font-size: 22px">Test</div> <div style="font-size: 24px">Test</div> </div> <div class="t-1 copy"> <div>Test</div> <div>Test</div> <div>Test</div> </div> <div class="t-2 copy"> <div>Test</div> <div>Test</div> <div>Test</div> </div>
You can do it like this: 您可以这样做:
$('.original').click(function() { var original_divs = $(this).find('div'), index = $(this).index('.original'); $('.copy').eq(index).find('div').each(function(i) { $(this).css('font-size', original_divs.eq(i).css('font-size')); }); })
/* Your original CSS */ div,div>div{display:inline-block}div{width:100%}div>div{content:"";width:30px;height:30px;float:left}.original{background:#bbb;cursor:pointer}.original>div{padding-right:10px}.copy{width:100%;display:block;margin-top:20px;clear:both}.copy>div{font-size:10px}
<!-- Your original HTML --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div class="t-1 original"> <div style="font-size: 14px">Test</div><div style="font-size: 16px">Test</div><div style="font-size: 18px">Test</div></div><div class="t-2 original"> <div style="font-size: 20px">Test</div><div style="font-size: 22px">Test</div><div style="font-size: 24px">Test</div></div><div class="t-1 copy"> <div>Test</div><div>Test</div><div>Test</div></div><div class="t-2 copy"> <div>Test</div><div>Test</div><div>Test</div></div>
You can Try like this 你可以这样尝试
$('.original').click(function() { $('.'+$(this).data('target')).html($(this).html()); })
div { display: inline-block; width: 100%; } div > div { display: inline-block; content: ""; width: 50px; height: 30px; float: left; } .original { background: #bbb; cursor: pointer; } .original > div { padding-right: 10px; } .copy { width: 100%; display: block; margin-top: 20px; clear: both; } .copy > div { font-size: 10px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="t-1 original" data-target="t-1-paste"> <div style="font-size: 14px">Test</div> <div style="font-size: 16px">Test</div> <div style="font-size: 18px">Test</div> </div> <div class="t-2 original" data-target="t-2-paste"> <div style="font-size: 20px">Test</div> <div style="font-size: 22px">Test</div> <div style="font-size: 24px">Test</div> </div> <div class="t-1-paste"> <div>Test</div> <div>Test</div> <div>Test</div> </div> <div class="t-2-paste"> <div>Test</div> <div>Test</div> <div>Test</div> </div>
Note: Added
data-target
attribute for paste target.注意:为粘贴目标添加了
data-target
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.