使用jquery,我如何选择一个div内的图像来改变它的来源 [英] using jquery, how can i select an image inside of a div to change its source

查看:79
本文介绍了使用jquery,我如何选择一个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>');

这将使用事件选择所有元素,类,然后找到他们的孩子 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.

  • .children(): http://api.jquery.com/children
  • .attr() : http://api.jquery.com/attr

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

这篇关于使用jquery,我如何选择一个div内的图像来改变它的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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