我有以下div,我知道DIV的选择器ID。
<div class="event"><img src="/Content/Images/Icons/calendar16.png">Event Name</div>
但我不知道图像是什么。我需要一些东西来找到div中的图像选择器,所以我可以将图像的来源更改为新图像。
答案 0 :(得分:38)
$('.event').children('img').attr('src', '<source here>');
这将选择具有event
类的所有元素,然后查找其子img
元素。如果您有多个匹配项并希望以不同方式更改其来源,则可以使用$.each()
来迭代它们。
.children()
:http://api.jquery.com/children .attr()
:http://api.jquery.com/attr 答案 1 :(得分:10)
$('div.event img').attr('src', '/anything');
如果您有几个以event
作为班级的div,如果您不想更改所有图片的来源,最好选择div作为ID