简体   繁体   中英

Selecting first image in div and replacing image src

I am trying to find a way to select the first image found within a div and replace the image source. It specifically must be this method as the image doesn't have an ID (complicated to explain).

$('.promo-unit-site-logo-3').find('img:first').attr'('src', 'blank')')';

I have tried using the above code from googling around and clipping something together however I am by way of javascript a novice.

Any help appreciated!

Thanks, John

see its working. you mistake in code.

 var image = $('.promo-unit-site-logo-3').find('img:first').attr('src', 'blank'); console.log(image.attr('src'));
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="promo-unit-site-logo-3"> <img src="http://placehold.it/280x210" title="first image"> <img src="http://placehold.it/30x20" title="Second image"> </div>

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