jQuery 查找元素的属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9032618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:13:46  来源:igfitidea点击:

Find element's attribute value

jquery

提问by tanya

I have got the following code :

我有以下代码:

  <ul>
    <li class="jstree-leaf" kids="0" range="5-7" name="mars" public_id="mars_05" ra_depth="5">
        <a href="#"><ins class="jstree-icon">&nbsp;</ins>mars</a>
    </li>
    <li class="jstree-leaf" kids="0" range="8-10" name="pluto" public_id="pluto_8" ra_depth="5">
        <a href="#"><ins class="jstree-icon">&nbsp;</ins>pluto</a>
    </li>
  </ul>

I need to get the ra_depthattribute value of a particular <li>by using it's nameattribute value.

我需要通过使用它的属性值来获取ra_depth特定<li>name属性值。

I tried the code below but it's not working:

我尝试了下面的代码,但它不起作用:

alert(li[name='"+myarray[0]+"'].attr("ra_depth"));

myarray[0]contains the value "pluto".

myarray[0]包含值“冥王星”。

Is there anything I am missing?

有什么我想念的吗?

回答by dknaack

Description

描述

You forget $(in your selector. Check out my sample and this jsFiddle Demonstration

你忘记$(了你的选择器。查看我的示例和这个jsFiddle 演示

Sample

样本

 alert($("li[name='"+myarray[0]+"']").attr("ra_depth"));

回答by Supr

alert($('li[name="'+myarray[0]+'"]').attr("ra_depth"));

回答by Richard Dalton

You are missing the actually jQuery function around your selector:

您缺少选择器周围的实际 jQuery 函数:

alert($("li[name='"+myarray[0]+"']").attr("ra_depth"));

回答by Rajeev Beakta

you missed the element seletor here.Try this

你在这里错过了元素选择器。试试这个

alert($("li[name='"+myarray[0]+"']").attr("ra_depth"));

alert($("li[name='"+myarray[0]+"']").attr("ra_depth"));