使用Jquery选择Element属性

时间:2018-03-14 16:58:54

标签: jquery

<ul class="top singleSelect" id="g-dropdown" data-type="singleSelect" role="listbox">
    <li id="li-0" data-level-id="10001" class="li-level1" tabindex="0">
        <ul id="ul-0">
           <li tabindex="-1" data-level-id="" id="li-0-0" class="li-level2">

我需要选择内部li的属性data-level-id

有人可以帮我用jquery选择元素吗?

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
//select all children li that have a data-level-id attribute on them
var elements = $('#g-dropdown').find('li[data-level-id]').filter(function() {
  //exclude elements that have a nested li with a level id
  return $(this).find('li[data-level-id]').length < 1;
});

console.log(elements.get());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="top singleSelect" id="g-dropdown" data-type="singleSelect" role="listbox">
  <li id="li-0" data-level-id="10001" class="li-level1" tabindex="0">
    <ul id="ul-0">
      <li tabindex="-1" data-level-id="" id="li-0-0" class="li-level2">
        Find Me
      </li>
    </ul>
  </li>
</ul>
&#13;
&#13;
&#13;