我有以下 html,我想在父 div 类中添加类,即 class="boxgrid",具体取决于teaser_title...
<div class="boxgrid">
<img src="my_image1.jpg" alt="Image 1">
<div class="cover boxcaption">
<div class="teaser-title">Image 1</div>
</div>
</div>
<div class="boxgrid">
<img src="my_image2.jpg" alt="Image 2">
<div class="cover boxcaption">
<div class="teaser-title">Image 2</div>
</div>
</div>
现在,我尝试了这段代码......
$('.teaser-text').each(function() {
var teasertext = $('.teaser-text').text().replace(/ /g,"_").toLowerCase();
$('.boxgrid').addClass(teasertext);
});
上面的代码输出如下。
<div class="boxgrid **image_1image_2**">
<img src="my_image1.jpg" alt="Image 1">
<div class="cover boxcaption">
<div class="teaser-title">Image 1</div>
</div>
</div>
我做错什么了?任何愿意帮助我解决这个小问题的人。谢谢..