【发布时间】:2017-02-21 21:17:28
【问题描述】:
如何在每个循环的 thr jquery 中附加有关特定类的内容。
我想在每个 ppp 类的每个 strong>Hello 中附加。
但是这段代码不起作用$(item)(.'ppp').append( "<strong>Hello</strong>" );
这里是完整的例子
$('.testimonial').each(function(index, item) {
var im = $('img').attr('src');
var iml = im.lenght;
var divpp = $('.ppp');
if (parseInt($(item).data('index')) > 0) {
$(item).html('Testimonial ' + (index + 1) +
'<span class="img-code"><input type="text" value=\'' + im + '\'></span> <br>');
$(item)(.
'ppp').append("<strong>Hello</strong>");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<div class="testimonial" data-index="1">
Testimonial
<div class="ppp" style="background-color:purple;"></div>
<img src="upload/IMG_20160721_143235(1).jpg" class="imgpfad" width="250px">
</div>
<div class="testimonial" data-index="2">
Testimonial 2
<div class="ppp" style="background-color:purple;"></div>
<img src="upload/IMG_20160721_143235(2).jpg" class="imgpfad" width="250px">
</div>
<div class="testimonial" data-index="3">
Testimonial 3
<img src="upload/IMG_20160721_143235(3).jpg" class="imgpfad" width="250px">
<div class="ppp"></div>
</div>
<div class="testimonial" data-index="4">
Testimonial 4
<img src="upload/IMG_20160721_143235(4).jpg" class="imgpfad" width="250px">
<div class="ppp"></div>
</div>
<div class="testimonial" data-index="5">
Testimonial 5
<img src="upload/IMG_20160721_143235(5).jpg" class="imgpfad" width="250px">
<div class="ppp"></div>
</div>
【问题讨论】:
-
我很好奇,你希望
$(item)(.'ppp')做什么?这是无效的语法... -
我该怎么做 $(item, .'ppp') 不起作用
-
需要是
$(item).find(.'ppp'),但是当你使用$(item).html('...')时你会清除这些元素中的所有内容,这样就不会找到任何.ppp -
它可能也应该是
$(item).find('img').attr('src')。否则每次循环都会得到第一张图片的src。 -
还有
lenght != length
标签: jquery html class append each