【问题标题】:Copy value from one element to another将值从一个元素复制到另一个元素
【发布时间】:2017-02-05 10:28:30
【问题描述】:

我有两对元素。第一对是.original,它包含子元素,每个子元素都有特定的字体大小。第二个是.copy,它的子元素中没有定义字体大小。

当用户单击.original 元素之一时,它应该将css font-size 从其子元素复制到具有相同索引的.copy 元素的子元素。例如,如果用户单击第一个 .original 元素,则 font-size 值应复制到第一个 .copy 元素的子元素。

目前我只能用下面的循环复制最后一个字体大小值。

$('.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>

【问题讨论】:

    标签: javascript jquery html foreach


    【解决方案1】:

    你可以这样做:

    $('.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&gt;div{display:inline-block}div{width:100%}div&gt;div{content:"";width:30px;height:30px;float:left}.original{background:#bbb;cursor:pointer}.original&gt;div{padding-right:10px}.copy{width:100%;display:block;margin-top:20px;clear:both}.copy&gt;div{font-size:10px}
    &lt;!-- Your original HTML --&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;&lt;div class="t-1 original"&gt; &lt;div style="font-size: 14px"&gt;Test&lt;/div&gt;&lt;div style="font-size: 16px"&gt;Test&lt;/div&gt;&lt;div style="font-size: 18px"&gt;Test&lt;/div&gt;&lt;/div&gt;&lt;div class="t-2 original"&gt; &lt;div style="font-size: 20px"&gt;Test&lt;/div&gt;&lt;div style="font-size: 22px"&gt;Test&lt;/div&gt;&lt;div style="font-size: 24px"&gt;Test&lt;/div&gt;&lt;/div&gt;&lt;div class="t-1 copy"&gt; &lt;div&gt;Test&lt;/div&gt;&lt;div&gt;Test&lt;/div&gt;&lt;div&gt;Test&lt;/div&gt;&lt;/div&gt;&lt;div class="t-2 copy"&gt; &lt;div&gt;Test&lt;/div&gt;&lt;div&gt;Test&lt;/div&gt;&lt;div&gt;Test&lt;/div&gt;&lt;/div&gt;

    【讨论】:

      【解决方案2】:

      你可以这样尝试

      $('.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>

      注意:为粘贴目标添加了data-target 属性。

      【讨论】:

      • 是的,我知道,但我的数据/测试可能会改变,所以我只需要复制 css 值。
      • 啊!我懂了!好的(y)@g5wx
      猜你喜欢
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 2012-09-27
      • 2011-01-21
      • 2020-11-06
      相关资源
      最近更新 更多