Javascript JQuery-$(';#test ul>;li';).index(2.hide();-可能的

Javascript JQuery-$(';#test ul>;li';).index(2.hide();-可能的,javascript,jquery,indexing,html-lists,Javascript,Jquery,Indexing,Html Lists,我能如此轻松地处理索引值吗?我认为使用自然索引值比使用类更好。我想这样使用.index Html 我找不到一点线索,如何以这种方式处理.index值。。使用此方法是否可以如此轻松地工作???您将其与整数(数字)一起使用,但只能与选择器或元素一起使用 编辑: 您可以这样编写自己的函数: function indexValue(index) { $(element).each(function(key, val) { if(key+1 == index) // + 1 bec

我能如此轻松地处理索引值吗?我认为使用自然索引值比使用类更好。我想这样使用.index

Html


我找不到一点线索,如何以这种方式处理.index值。。使用此方法是否可以如此轻松地工作???

您将其与整数(数字)一起使用,但只能与选择器或元素一起使用

编辑:
您可以这样编写自己的函数:

function indexValue(index)
{
    $(element).each(function(key, val) {
        if(key+1 == index) // + 1 because you started from 0
        {
            return val;
        }
    });
}
您可以使用:

它也可以是选择器的一部分:

$('#test ul > li:eq(2)').hide();
$('#test ul > li:eq(2)').hide();
如果您希望在
#test
中的所有
ul
中使用第三个
li
,您可以使用(注意它是基于1的):


是的,你可以。但是,您需要
.eq()
,而不是
.index()

或作为选择器的一部分:

$('#test ul > li:eq(2)').hide();
$('#test ul > li:eq(2)').hide();
$(文档).ready(函数(){
$(“按钮1”)。单击(函数(){
$(“p”).hide();
});
});
$(文档).ready(函数(){
$(“按钮2”)。单击(函数(){
$(“p”).show();
});
});
阴蒂
沃尔塔我
$('#test ul > li:nth-child(3)').hide();
$('#test ul > li').eq(2).hide();
$('#test ul > li:eq(2)').hide();
$(document).ready(function(){
  $("button#1").click(function(){
    $("p").hide();
  });
});
$(document).ready(function(){
  $("button#2").click(function(){
    $("p").show();
  });
});
<button id="1">Clica-me</button>
<button id="2">Volta-me</button>