我是js的新手,只學了一週左右,所以請友好。下面的腳本改變了iframe的src,以便在單擊.thumb類的鏈接時匹配同一iframe的自定義數據屬性。JQuery - 將變量的值傳遞給類的索引值
這工作正常,但它目前正在改變所有iframe的所有類的iframe src與.gallery的父級div類。我只是想用與'clicked'變量相同的索引值來更改.gallery的iframe src。
//When an link with a class of .thumb is clicked
$(document).on('click', '.thumb', function() {
//variable index is the index of the links parent 'li'
var clicked = $(this).parent('li').index();
// Find iFrames of all .comm-gallery elements and alter their src to iframes data-src value
$('.gallery').find("iframe").prop("src", function(){
// Set their src attribute to the value of data-src
return $(this).data("src");
});
});
我已經試過在過去的幾個小時無數循環的解決方案,但沒有工作過,正如我不太瞭解這一點的JS,我想,我在這的最小公分母。任何幫助將不勝感激!返回數據-SRC之前
你嘗試'像'$ .eq'( '畫廊')。找到( 「IFRAME」)。EQ(點擊).prop(..'? – 2013-03-11 17:45:18
已經接受了對方的回答,但是這 – 2013-03-11 18:09:37
如果'.eq'對你有用,那麼我建議你就這樣做,它更像是迭代1項和迭代所有項目並比較索引。 – 2013-03-11 18:22:39