我是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