我可以将:eq()选择器与变量一起使用吗?或者我必须使用替代品......但我可以使用哪种替代品? 对于上下文,我使用了一个库。
HTML
<div id="#thumbs">
<ul>
<li>Thumbnail 1</li>
<li>Thumbnail 2</li>
<li>Thumbnail 3</li>
</ul>
</div>
<div id="#gallery">
<ul>
<li>Image 1</li> <!-- User will see this 800px x 450px image -->
<li>Image 2</li>
<li>Image 3</li>
</ul>
</div>
伪Javascript(JQuery)
$('#thumbs ul li').click(function() {
clickedPos = $(this).index(); //saving the index number of the clicked thumbnail
$('#gallery ul li:eq(clickedPos)').clone().insertAfter('#gallery ul li:eq(clickedPos)');
gallerywidth += 800 //gallery width grows a bit
$('#gallery ul').css({width: gallerywidth});
$('#gallery ul').animate({right:"+=800"},250);
});
互动示例
USER点击缩略图3
HTML结果
<div id="#thumbs">
<ul>
<li>Thumbnail 1</li>
<li>Thumbnail 2</li>
<li>Thumbnail 3</li>
</ul>
</div>
<div id="#gallery">
<ul>
<li>Image 1</li> <!-- at first the user saw this image -->
<li>Image 3</li> <!-- after scrolling 800px to the right, he now see this one -->
<li>Image 2</li>
<li>Image 3</li>
</ul>
</div>
答案 0 :(得分:4)
使用:
$('#gallery ul li:eq('+clickedPos+')')
或:
$('#gallery ul li').eq(clickedPos)
答案 1 :(得分:0)
为什么不使用$('#gallery ul li:eq(' + clickedPos + ')')