2014-11-04 119 views
0

jquery noob,所以不要太費勁地試試plz。 在產品描述中獲得帶有表格的產品網格。表td具有相同的類,當我嘗試從其中的每一箇中拉出內容並插入到不同的位置時,我仍然會獲得相同的內容,這些內容在每個產品中都有所不同。jquery:從同一類別的不同div中拉出不同的內容

這裏是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); 
    }); 
+0

瓦在'.product'和'.product777'?編輯了 – 2014-11-04 08:32:00

+0

。搞砸了簡化代碼 – whaaaaaaz 2014-11-04 08:38:50

回答

0

我認爲這可以幫助你:

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

+0

的確如此。謝謝! – whaaaaaaz 2014-11-04 08:45:55