如果元素有子元素html元素

If element has children html elements

本文关键字:元素 html 如果      更新时间:2023-09-26

下面是无序列表

<ul>
      <li>
        <a class="hlink hlink-1" href="#"> Prank Boss Apps </a>
        <ul> 
          <li> <a href="#"> link 1 </a></li>
          <li> <a href="#"> link 2 </a></li>
          <li> <a href="#"> link 3 </a></li>
        </ul>
      </li>
      <li>
        <a class="hlink hlink-2" href="#"> Uninstall an app.  </a>
      </li>
      <li>
        <a class="hlink hlink-3" href="#"> Contact Us </a>
      </li>
    </ul>

在无序列表中,不是每个列表项都有另一个无序列表。

          <li> <a href="#"> blah </a>
            <ul> 
              <li> <a href="#"> link 1 </a></li>
              <li> <a href="#"> link 2 </a></li>
              <li> <a href="#"> link 3 </a></li>
            </ul>
          </li>

所以有些会在列表项中有一个链接,而有些会在列表项中有一个无序的列表。

如何检查列表项是否在其内部没有另一个无序列表?

 function hasChildULs(thisList)
 {    
      if ($(thisList).children('ul').length > 0)
      {
           return true;
      }
      else
      {
           return false;
      }
 }

一旦你有了对li元素的引用,你就可以使用这个了。

function isLeafNode(liElement) {
   return !liElement.getElementsByTagName("ul").length;
}

这应该让你开始:http://www.w3schools.com/dom/dom_element.asp(如果你不想使用jQuery)