这是我的html结构,
<div class="productbox">
<div class"result1 prod" name="BGFDTTTTJ">
<h2>Product 1 Title</h2>
<div class="nameattrib"></div>
</div>
<div class"result2 prod" name="BGFDTTTTJ">
<h2>Product 2 Title</h2>
<div class="nameattrib"></div>
</div>
...
</div>
这是我的javascript
$(document).ready(function() {
$(".productbox .prod").each(function(index, value){
$(".nameattrib").append(" " + $(this).attr('name'));
});
});
因此我得到一个html结构,
<div class="productbox">
<div class"result1 prod" name="BGFDTTTTJ">
<h2>Product 1 Title</h2>
<div class="nameattrib">BGFDTTTTJ BHGFGFGG </div>
</div>
<div class"result2 prod" name="BHGFGFGG">
<h2>Product 2 Title</h2>
<div class="nameattrib">BGFDTTTTJ BHGFGFGG</div>
</div>
...
</div>
我需要进行哪些修改以避免打印所有名称值,即。我需要得到类似于下面的结果。
<div class="productbox">
<div class"result1 prod" name="BGFDTTTTJ">
<h2>Product 1 Title</h2>
<div class="nameattrib">BGFDTTTTJ</div>
</div>
<div class"result2 prod" name="BHGFGFGG">
<h2>Product 2 Title</h2>
<div class="nameattrib">BHGFGFGG</div>
</div>
...
</div>
答案 0 :(得分:0)
将javascript更改为:
$(document).ready(function() {
$(".productbox .prod").each(function(index, value){
$(this).find(".nameattrib").text($(this).attr('name'));
});
});