简体   繁体   中英

How to access img src in div tags

I am just wondering how I can access the img src (ID wows1_0) that is located within two div tags and then be able to change the src via javascript.

<div id="wowslider-container1" class="grid_7 alignGallery">
<div class="ws_images"><ul>
        <li><img src="WOWSlider/data1/images/BookARoom.jpg" alt="full screen slider" title="full screen slider" id="wows1_0"/></li>
        <li><img src="WOWSlider/data1/images/ContactSupport.jpg" alt="ContactSupport" title="ContactSupport" id="wows1_1"/></li>
    </ul></div>
<div class="ws_thumbs">
    <div>
        <a href="#wows1_0" title="full screen slider"><img src="WOWSlider/data1/tooltips/bookaroom.jpg" alt="" /></a>
        <a href="#wows1_1" title="ContactSupport"><img src="WOWSlider/data1/tooltips/contactsupport.jpg" alt="" /></a>
    </div>
</div>               
<div class="ws_shadow"></div>
</div>      

If you are using jQuery, then:

$(".ws_images img").each(function() {
     console.log($(this).attr("src"));
     $(this).attr("src", value); // to set it
});

Yes you can do by using pure javascript code. We can access it by using id of img with getElementById method and change the src using src property.

var image=document.getElementById("wows1_0");
image.src="ss.jpg";

check this fiddle

http://jsfiddle.net/9rb3ahs3/1/

在此处输入图片说明

如果您将img元素作为具有id属性a元素的唯一子元素,则可以很简单地访问它,例如

document.getElementById("wows1_0").getElementsByTagName('img')[0].src

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM