如何使用 jquery 获取元素的 id、class 或 name attr

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13434973/
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-26 12:37:05  来源:igfitidea点击:

How can I get id,class or name attr an element with jquery

jqueryattrhtml

提问by regxcode

I'm new in jquery. How can I get name,ID or class name an element with jquery.I'm trying as;

我是 jquery 的新手。如何使用 jquery 获取元素的名称、ID 或类名。我正在尝试;

<div class="className" onclick="getname();"></div>
<div id="idName" onclick="getName();"></div>
<div name="attrName" onclick="getName()"></div>

function getName(){
attrName = $(this).attr("name");
alert(attrName);
}

but doesn't work

但不起作用

回答by Rory McCrossan

Firstly using jQuery you should attach click()handlers rather than use antiquated onclickattributes. Then you can use the thiskeyword in the manner you have in your function.

首先使用 jQuery 你应该附加click()处理程序而不是使用过时的onclick属性。然后您可以this按照您在函数中的方式使用关键字。

Try this:

尝试这个:

<div class="className"></div>
<div id="idName"></div>
<div name="attrName"></div>
$(function() {
    $("div").click(function() {
        var name = this.name;
        var cls = this.className;
        var id= this.id;

        alert("Name: " + name + " / Class: " + cls + " / Id: " + id);
    });
});

Example fiddle

示例小提琴

The attributes you've selected are all available through POJS, you could also convert thisto a jQuery object to get other attributes, for example:

您选择的属性都可以通过 POJS 获得,您也可以转换this为 jQuery 对象以获取其他属性,例如:

var title = $(this).attr('title');
var myattribute = $(this).data('myattribute'); // using HTML5: <div data-myattribute="foo"></div>

回答by Minko Gechev

Try this:

尝试这个:

JavaScript

JavaScript

$(function () {
    $('div').click(function () {
        var elem = $(this);
        alert('Class: ' + elem.attr('class'));
        alert('Id: ' + elem.attr('id'));
        alert('Name: ' + elem.attr('name'));    
    });????
});

HTML

HTML

<div class="className" onclick="getname();"></div>
<div id="idName" onclick="getName();"></div>
<div name="attrName" onclick="getName()"></div>

CSS

CSS

?div {
    width: 100px;
    height: 100px;
    border: 1px solid black;
}?

JSfiddle: http://jsfiddle.net/54ynV/

JSfiddle:http: //jsfiddle.net/54ynV/

In the script above we're attaching to the click event of every div on the page $('div').click.... In the callback we're getting it's attributes.

在上面的脚本中,我们附加到页面上每个 div 的点击事件$('div').click...。在回调中,我们获得了它的属性。

回答by j08691

You need to pass a reference to the element you want to get the name attribute of. Try this:

您需要传递对要获取其名称属性的元素的引用。尝试这个:

jQuery

jQuery

function getName(me) {
    attrName = $(me).attr("name");
    alert(attrName);
}?

HTML

HTML

<div class="className" onclick="getname(this);">a</div>
<div id="idName" onclick="getName(this);">b</div>
<div name="attrName" onclick="getName(this)">c</div>?

jsFiddle example

jsFiddle 示例

Now you should get the name of the last element since that's the only element that has a name. The others will return undefined.

现在您应该获得最后一个元素的名称,因为这是唯一具有名称的元素。其他人会回来的undefined