javascript - JQuery - 使用 :eq(clickedPos) selector with a variable

标签 javascript jquery selector html-lists equals

我可以将 :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);   
});

交互示例
用户点击缩略图 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>

最佳答案

用途:

$('#gallery ul li:eq('+clickedPos+')')

或者:

$('#gallery ul li').eq(clickedPos)

关于javascript - JQuery - 使用 :eq(clickedPos) selector with a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4602099/

相关文章:

javascript - Angular JS 不适用于 IE9 但适用于其他浏览器

javascript - 如何减少这个脚本

javascript - 仅将颜色应用于数组的一部分?

javascript - 如何在 keyup 上调用我的函数?

php - 如何简化我的国家选择菜单 PHP/mysql

javascript - VueJS - 列出数组并独立显示/隐藏

javascript - 语法错误: "Cannot use import statement outside a module" - JavaScript in Python

javascript - 尝试将一些 javascript 放入 ASP.NET 页面

javascript - 如何使用jquery用鼠标移动div?

javascript - CSS 选择器相当于backbone.js 中的jQuery.find?