如何在div中选择图像以更改其来源? [英] How can I select an image inside of a div to change its source?

查看:50
本文介绍了如何在div中选择图像以更改其来源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下div,我知道DIV的选择器ID.

I have the following div and I know the selector Id of the DIV.

 <div class="event"><img src="/Content/Images/Icons/calendar16.png">Event Name</div>

但我不知道图像是什么.我需要一些东西来在div中找到图像选择器.因此,我可以将图像来源更改为新图像.

but I don't know, what the image is. I need something to find the image selector inside the div, that I have. So I can go change the source of the image to a new image.

推荐答案

$('.event').children('img').attr('src', '<source here>');

这将选择具有 event 类的所有元素,然后找到其子元素 img 元素.如果您有多个匹配项,并且想要以不同的方式更改其来源,则可以使用 $.each()进行遍历.

This selects all the elements with the event class and then finds their children img elements. If you have multiple matches and want to change their sources differently then you can use $.each() to iterate through them.

演示: http://jsfiddle.net/aPzgR/

这篇关于如何在div中选择图像以更改其来源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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