using jquery, how can i select an image inside of a div to change its source

大兔子大兔子 提交于 2019-12-04 10:08:53

问题


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>

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.


回答1:


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

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

A demo: http://jsfiddle.net/aPzgR/




回答2:


$('div.event img').attr('src', '/anything');

If you have several divs with event as the class you're better off selecting the div by id if you don't want to change the source of all the images



来源:https://stackoverflow.com/questions/8440315/using-jquery-how-can-i-select-an-image-inside-of-a-div-to-change-its-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!