如何使用jQuery获取元素名称?

时间:2022-04-25 04:42:19

How can I get name property of HTML element with jQuery?

如何使用jQuery获取HTML元素的name属性?

6 个解决方案

#1


157  

You should use attr('name') like this

你应该像这样使用attr('name')。

 $('#yourid').attr('name')

you should use an id selector, if you use a class selector you encounter problems because a collection is returned

您应该使用id选择器,如果您使用类选择器,您会遇到问题,因为返回了集合

#2


9  

$('someSelectorForTheElement').attr('name');

#3


7  

To read a property of an object you use .propertyName or ["propertyName"] notation.

要读取一个对象的属性,请使用.propertyName或["propertyName"]符号。

This is no different for elements.

这对元素来说也没什么不同。

var name = $('#item')[0].name;
var name = $('#item')[0]["name"];

If you specifically want to use jQuery methods, then you'd use the .prop() method.

如果您特别想使用jQuery方法,那么您应该使用.prop()方法。

var name = $('#item').prop('name');

Please note that attributes and properties are not necessarily the same.

请注意,属性和属性不一定相同。

#4


2  

var name = $('#myElement').attr('name');

#5


1  

Play around with this jsFiddle example:

使用这个jsFiddle示例:

HTML:

HTML:

<p id="foo" name="bar">Hello, world!</p>

jQuery:

jQuery:

$(function() {
    var name = $('#foo').attr('name');

    alert(name);
    console.log(name);
});

This uses jQuery's .attr() method to get value for the first element in the matched set.

这使用jQuery的.attr()方法获取匹配集中第一个元素的值。

While not specifically jQuery, the result is shown as an alert prompt and written to the browser's console.

虽然不是jQuery,但结果显示为警告提示符,并写入浏览器控制台。

#6


0  

The method .attr() allows getting attribute value of the first element in a jQuery object:

方法.attr()允许获取jQuery对象中第一个元素的属性值:

$('#myelement').attr('name');

#1


157  

You should use attr('name') like this

你应该像这样使用attr('name')。

 $('#yourid').attr('name')

you should use an id selector, if you use a class selector you encounter problems because a collection is returned

您应该使用id选择器,如果您使用类选择器,您会遇到问题,因为返回了集合

#2


9  

$('someSelectorForTheElement').attr('name');

#3


7  

To read a property of an object you use .propertyName or ["propertyName"] notation.

要读取一个对象的属性,请使用.propertyName或["propertyName"]符号。

This is no different for elements.

这对元素来说也没什么不同。

var name = $('#item')[0].name;
var name = $('#item')[0]["name"];

If you specifically want to use jQuery methods, then you'd use the .prop() method.

如果您特别想使用jQuery方法,那么您应该使用.prop()方法。

var name = $('#item').prop('name');

Please note that attributes and properties are not necessarily the same.

请注意,属性和属性不一定相同。

#4


2  

var name = $('#myElement').attr('name');

#5


1  

Play around with this jsFiddle example:

使用这个jsFiddle示例:

HTML:

HTML:

<p id="foo" name="bar">Hello, world!</p>

jQuery:

jQuery:

$(function() {
    var name = $('#foo').attr('name');

    alert(name);
    console.log(name);
});

This uses jQuery's .attr() method to get value for the first element in the matched set.

这使用jQuery的.attr()方法获取匹配集中第一个元素的值。

While not specifically jQuery, the result is shown as an alert prompt and written to the browser's console.

虽然不是jQuery,但结果显示为警告提示符,并写入浏览器控制台。

#6


0  

The method .attr() allows getting attribute value of the first element in a jQuery object:

方法.attr()允许获取jQuery对象中第一个元素的属性值:

$('#myelement').attr('name');