Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何仅选择顶部<;李>;使用jQuery_Jquery - Fatal编程技术网

如何仅选择顶部<;李>;使用jQuery

如何仅选择顶部<;李>;使用jQuery,jquery,Jquery,这是一个html。我不在乎它是否有效,我只想使用jQuery从中查询“title to select”,仅此而已 <div class="my class1213"> <h3> just title, no selection</h3> <ul> <li>title to select</li> <li>title to select <

这是一个html。我不在乎它是否有效,我只想使用jQuery从中查询“title to select”,仅此而已

<div class="my class1213">
    <h3> just title, no selection</h3>
    <ul>
        <li>title to select</li>
        <li>title to select
            <ul>
                <li>nonono</li>
            </ul>
        </li>
        <li>title to select
           <ul>
             <li>
                 <ul>
                     <li>no selection</li>
                 </ul>
             </li>                 
          </ul>     
        </li>    
    </ul>    

</div>​​​​

只有标题,没有选择
  • 要选择的标题
  • 要选择的标题
    • 诺诺诺
  • 要选择的标题
      • 没有选择
​​​​
我怎么做

$('.my ul li:first');
应该足够了

备选方案:

$('.my ul li:eq(0)');

$('.my ul li:nth-child(1)');
根据未定义的注释进行编辑:

你可以使用他的解决方案,或者类似的方法:

$('.my ul li').filter(function(){
    return $(this).text === 'title to select';
});
应该足够了

备选方案:

$('.my ul li:eq(0)');

$('.my ul li:nth-child(1)');
根据未定义的注释进行编辑:

你可以使用他的解决方案,或者类似的方法:

$('.my ul li').filter(function(){
    return $(this).text === 'title to select';
});

您可以使用CSS选择器,也可以使用函数

备选方案a:

$('.class1213 ul li:first-child')
// selects only the first li child of ul
备选案文b:

$('.class1213 ul li').first()
// first selects all li's, then only uses the first element found

您可以使用CSS选择器,也可以使用函数

备选方案a:

$('.class1213 ul li:first-child')
// selects only the first li child of ul
备选案文b:

$('.class1213 ul li').first()
// first selects all li's, then only uses the first element found

@SeToY应该在使用jquery而不是作为纯css时工作selector@undefined啊,好的,我用另一种可能的解决方案进行了更新。仅限IE9中的第n-child,而不是下面的。我昨天不得不自己处理这件事。请参阅:@setoyjquery的选择器引擎(Sizzle)隐式地为您处理它。在ie7模式下试试这个小把戏:@SeToY应该在您使用jquery时起作用,而不是作为一个纯cssselector@undefined啊,好的,我用另一种可能的解决方案进行了更新。仅限IE9中的第n-child,而不是下面的。我昨天不得不自己处理这件事。请参阅:@setoyjquery的选择器引擎(Sizzle)隐式地为您处理它。在ie7模式下尝试此小提琴: