JQuery:帮助使用 .each() 和 .append() 将图片添加到 HTML [英] JQuery: Help using .each() and .append() to add pictures to HTML

查看:24
本文介绍了JQuery:帮助使用 .each() 和 .append() 将图片添加到 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要修复的简单错误,我无法弄清楚出了什么问题.我需要将相同的图片附加到 HTML 中的多个(五个)div.出于某种原因,我的代码向每个 div 附加了五次相同的图片.更清楚的是,五个 div 中的每一个都需要一张图片.现在,这五个人各有五张照片.这是 JQUERY:

Simple bug that needs to be fixed and I can't figure out what's wrong. I need to append the same picture to multiple (five) divs in the HTML. For some reason, my code is appending the same picture five times to each div. Making it more clear, each of the five divs needs one picture. Right now, all five have five pictures each. Here is the JQUERY:

$(".faq").each(function(){
        $('.faq .letter-q').append('<img src="images/faq-q.png" alt="Question">');
});

这是插入的位置:

<div class="letter-q"></div>

其中五个分布在全身.

这可能是我遗漏的一些小东西.任何帮助将不胜感激!

It's probably something small I'm missing. Any help would be appreciated!

推荐答案

如果您想首先使用五个 .letter-q div,请先选择它们,以便.each"运行该函数时它正在使用那些 div:

If you want to work with the five .letter-q div's first then select them first so that ".each" time the function is run it is working with those div's:

$('.faq .letter-q').each(function(){
    //this wrapped in jQuery will give us the current .letter-q div
    $(this).append('<img src="images/faq-q.png" alt="Question">');
});

这篇关于JQuery:帮助使用 .each() 和 .append() 将图片添加到 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆