2014-12-02 140 views
-5

如何访问<li>jQuery is for chumps!</li>元素? 它没有ID,所以我不知道该怎么做。 我将它存储在一个变量访问特定的DOM元素

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Simplify, Simplify</title> 
     <script type='text/javascript' src='script.js'></script> 
    </head> 
    <body> 
     <div> Remember! 
      <ul> 
       <li> 
        <ol> 
         <li>Start with the function keyword</li> 
         <li>Inputs go between()</li> 
         <li>Actions go between {}</li> 
         <li>jQuery is for chumps!</li> 
        </ol> 
       </li> 
       <li>Inputs are separated by commas.</li> 
       <li>Inputs can include other functions!</li> 
      </ul> 
     </div> 
    </body> 
</html> 
+0

它可以像'$容易( '礼:包含( “jQuery是为傻瓜!”)' )'。你可以使用许多遍历方法,使用它们:https://api.jquery.com/category/traversing/ – 2014-12-02 08:05:22

+0

你想通过它的内容或位置来使用'li'吗? – 2014-12-02 08:05:24

+0

基于...... li的内容或其位置的选择是什么 – 2014-12-02 08:05:40

回答

1

你可以这样做:

var selector = $('ol li:last'); 

更具体会增加一个ID或类父DIV做像这样:

var selector = $('#parent ol li:last'); 

或者,如果它可以在你的OL,那么你可以使用在任何地方包括:

var selector = $('#parent ol li:contains("jQuery is for chumps!")'); 
+0

您正在寻找的li实际上是'ol'的最后一个孩子,而不是'ul' – webkit 2014-12-02 08:06:52

+1

不会选择'

  • 输入可以包含其他功能!
  • '? – 2014-12-02 08:06:53

    1

    你可以在这里使用多个选择

    //using the contents of the li, using contains() can return partial matches so need to be careful about it 
     
    $('ul ol li').filter(':contains("jQuery is for chumps!")').css('color', 'red'); 
     
    //using the position - it targets the last child of the ol element 
     
    $('ul ol li:last-child').css('background-color', 'green');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
     
    <div>Remember! 
     
        <ul> 
     
        <li> 
     
         <ol> 
     
         <li>Start with the function keyword</li> 
     
         <li>Inputs go between()</li> 
     
         <li>Actions go between {}</li> 
     
         <li>jQuery is for chumps!</li> 
     
         </ol> 
     
        </li> 
     
        <li>Inputs are separated by commas.</li> 
     
        <li>Inputs can include other functions!</li> 
     
        </ul> 
     
    </div>

    0

    没有jQuery的:document.getElementsByTagName('li')[5]

    然后你可以使用if语句来检查它是否有正确的内容。

    var i = 0; 
    while(i <= document.getElementsByTagName('li').length()){ 
        if(document.getElementsByTagName('li')[i].innerHTML == "jQuery is for Chumps"){ 
         //Do Something 
        } 
        i++; 
    } 
    
    0

    首先你的HTML,使查询速度更快,添加ID。否则,你将不得不从身体标签看,这可能需要一些时间。

      <ul id="menu-bar"> 
           <li> 
            <ol> 
             <li>Start with the function keyword</li> 
             <li>Inputs go between()</li> 
             <li>Actions go between {}</li> 
             <li>jQuery is for chumps!</li> 
            </ol> 
           </li> 
           <li>Inputs are separated by commas.</li> 
           <li>Inputs can include other functions!</li> 
          </ul> 
    

    而且现在的选择,无论是通过检查的最后一个节点

    var lastElement = $('#menu-bar > li > ol > li:last'); 
    

    或内容(如果您支持多国语言,将无法正常工作)

    var lastElement = $('#menu-bar > li > ol li:contains("jQuery is for chumps!")') 
    
    0

    获取直接儿童ol元素:

    var children = $("ol > li"); 
    

    获取特定元素:

    var element = children.last(); 
    

    获取从元素中的文本:

    var text = element.html();