[英]How to change the src of images which has no class/ID from JavaScript
[英]which image has class, change src JQUERY
我想更改具有類'activeLittle'的圖片的src
$('#sliderInfoBoxImages img').hasClass('activeLittle').attr('src', 'some.jpg');
錯誤: Uncaught TypeError: Object #<error> has no method 'attr'
您需要使用類選擇器
$('#sliderInfoBoxImages img.activeLittle').attr('src', 'some.jpg');
.hasClass()返回一個布爾結果,指示該元素是否具有已傳遞的類……該類沒有方法attr()
因此您會得到錯誤
函數.hasClass()將返回一個布爾值。 它不會返回任何元素。 順便說一句,您必須使用element with class/Id selector
來實現所需的功能。
嘗試,
$('#sliderInfoBoxImages img.activeLittle').attr('src', 'some.jpg');
嘗試這個,
$img = $('#sliderInfoBoxImages img');
if($img.hasClass('activeLittle')){
$img.attr('src', 'some.jpg');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.