获取元素ID

这个问题在这里已经有了答案:

  • 我如何使用jQuery获取元素的ID? 17个答案

  • var id = $('li[data-id="KnSubject[StId]"]').attr('id');
    console.log(id);
    

    使用attr函数。

    var elem = $('li[data-id="KnSubject[StId]"]');
    console.log(elem.attr('id'));
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <li role="treeitem" data-jstree="{&quot;icon&quot; : &quot;glyphicon glyphicon-pencil&quot;}" data-id="KnSubject[StId]" data-values="null" aria-selected="true" aria-level="3" aria-labelledby="j1_4_anchor" id="j1_4" class="jstree-node  jstree-leaf"><i class="jstree-icon jstree-ocl" role="presentation"></i><a class="jstree-anchor jstree-clicked" href="#" tabindex="-1" id="j1_4_anchor"><i class="jstree-icon jstree-themeicon glyphicon glyphicon-pencil jstree-themeicon-custom" role="presentation"></i>Type dossieritem (StId)</a></li>

    仅出于完整性原因:

    $('li[data-id="KnSubject[StId]"]').get(0).id
    

    .get(0)检索原始JS DOM元素。

    链接地址: http://www.djcxy.com/p/83297.html

    上一篇: Get Element ID

    下一篇: id property returns null or undefined