【问题标题】:jquery: pull different content from different divs with same classesjquery:从具有相同类的不同div中提取不同的内容
【发布时间】:2014-11-04 08:21:22
【问题描述】:

Jquery noob,所以请不要太用力。 在产品描述中获得带有表格的产品网格。表 td 具有相同的类,当我尝试从它们中提取内容并插入到不同的位置时,我仍然得到相同的内容,每个产品的内容都不同。

这里是html

<div class="product">
<div class="product777">
<table>
<tr><td class="varimage1">content 1</td><td class="varimage2">COntent2</td></tr>
</table>
<div class="galitem1"></div>
<div class="galitem2"></div>
</div></div>

这是一段js代码

$(".product").each(function(){
// tried this
var colorimgab = $(".varimage2").html();
//and tried this
var colorimgaa = $(this).closest(".product777").find(".varimage1").html();
$(".galitem1").html(colorimgaa);
$(".galitem2").html(colorimgab);
  });

【问题讨论】:

  • .product.product777 是什么?
  • 已编辑。搞砸了简化代码

标签: javascript jquery html


【解决方案1】:

我认为这可以帮助你:

$(".product").each(function(){
  var colorimgab = $(this).find(".varimage1").html();
  var colorimgaa = $(this).find(".varimage2").html();
  $(this).find(".galitem1").html(colorimgab);
  $(this).find(".galitem2").html(colorimgaa);
});
td{
  border:1px solid black;  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="product">
  <div class="product777">
    <table>
      <tr>
        <td class="varimage1">content 1</td>
        <td class="varimage2">COntent2</td></tr>
    </table>
    <div class="galitem1"></div>
    <div class="galitem2"></div>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2016-03-18
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    相关资源
    最近更新 更多