簡體   English   中英

哪個圖像具有類,更改src JQUERY

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM