19 回答 19

This answer is useful

2604

jQuery方式:

$('#test').attr('id')

在您的示例中:

$(document).ready(function() {

console.log($('#test').attr('id'));

});

或者通过 DOM:

$('#test').get(0).id;

甚至 :

$('#test')[0].id;

在 JQuery中使用的原因$('#test').get(0)甚至$('#test')[0]是$('#test')JQuery 选择器并通过其默认功能返回结果的数组()而不是单个元素

jquery 中 DOM 选择器的另一种选择是

$('#test').prop('id')

它不同于.attr()并$('#test').prop('foo')抓取指定的 DOMfoo属性,而$('#test').attr('foo')抓取指定的 HTMLfoo属性,您可以在此处找到有关差异的更多详细信息。

于 2010-07-13T17:12:47.747 回答

This answer is useful

91

$('selector').attr('id')将返回第一个匹配元素的 id。参考。

如果匹配的集合包含多个元素,则可以使用常规.each 迭代器返回包含每个 id 的数组:

var retval = []

$('selector').each(function(){

retval.push($(this).attr('id'))

})

return retval

或者,如果您愿意更坚韧一点,您可以避免使用包装器并使用.map 快捷方式。

return $('.selector').map(function(index,dom){return dom.id})

于 2010-07-13T17:13:11.680 回答

This answer is useful

42

id是 html 的属性Element。但是,当您编写 时$("#something"),它会返回一个包装匹配的 DOM 元素的 jQuery 对象。要取回第一个匹配的 DOM 元素,请调用get(0)

$("#test").get(0)

在这个原生元素上,您可以调用 id 或任何其他原生 DOM 属性或函数。

$("#test").get(0).id

这就是为什么id不在您的代码中工作的原因。

或者,使用 jQuery 的attr方法作为其他答案的建议来获取id第一个匹配元素的属性。

$("#test").attr("id")

于 2010-07-13T17:16:42.797 回答

This answer is useful

26

上面的答案很好,但随着 jquery 的发展......所以你也可以这样做:

var myId = $("#test").prop("id");

于 2013-12-05T16:46:08.740 回答

This answer is useful

23

$.fn.extend({

id : function() {

return this.attr('id');

}

});

alert( $('#element').id() );

当然需要一些检查代码,但很容易实现!

于 2014-09-25T13:15:36.667 回答

This answer is useful

9

.id不是有效的 jquery 函数。您需要使用该.attr()函数来访问元素拥有的属性。您既可以.attr()通过指定两个参数来更改属性值,也可以通过指定一个参数来获取值。

http://api.jquery.com/attr/

于 2010-07-13T17:17:19.423 回答

This answer is useful

9

如果你想获得一个元素的 ID,比如说通过类选择器,当一个事件(在本例中为 click 事件)在该特定元素上被触发时,那么以下将完成这项工作:

$('.your-selector').click(function(){

var id = $(this).attr('id');

});

于 2015-05-29T10:35:43.403 回答

This answer is useful

7

$('#test').attr('id')

在您的示例中:

$(document).ready(function() {

alert($('#test').attr('id'));

});

于 2017-03-27T09:46:21.123 回答

This answer is useful

5

好吧,似乎还没有解决方案,并想提出我自己的解决方案,即扩展 JQuery 原型的解决方案。我把它放在一个在 JQuery 库之后加载的 Helper 文件中,因此检查window.jQuery

if (window.jQuery) {

$.prototype.id = function () {

if (this.length > 1) {

var val = [];

this.each(function (idx, el) {

val.push($(el).id());

});

return val;

} else {

return this.attr('id');

}

}

}

它可能并不完美,但它可能是被纳入 JQuery 库的开始。

返回单个字符串值或字符串值数组。字符串值数组,用于使用多元素选择器的事件。

于 2017-02-09T18:27:51.050 回答

This answer is useful

4

$('#test')返回一个 jQuery 对象,所以你不能简单地使用object.id它来获取它Id

你需要使用$('#test').attr('id'),它返回你需要ID的元素

这也可以按如下方式完成,

$('#test').get(0).id这等于document.getElementById('test').id

于 2010-07-13T17:16:38.960 回答

This answer is useful

3

可能对找到此线程的其他人有用。下面的代码仅在您已经使用 jQuery 时才有效。该函数总是返回一个标识符。如果元素没有标识符,则函数生成标识符并将其附加到元素。

var generatedIdCounter = 0;

$.fn.id = function() {

var identifier = this.attr('id');

if(!identifier) {

generatedIdCounter++;

identifier = 'isGenerated_' + generatedIdCounter;

this.attr('id', identifier);

}

return identifier;

}

如何使用:

$('.classname').id();

$('#elementId').id();

于 2016-02-17T10:18:34.517 回答

This answer is useful

2

$('tagname').attr('id');

使用上面的代码,您可以获得 id。

于 2013-09-03T11:29:10.793 回答

This answer is useful

2

这是一个老问题,但截至 2015 年,这实际上可能有效:

$('#test').id;

您还可以进行分配:

$('#test').id = "abc";

只要你定义了下面的 JQuery 插件:

Object.defineProperty($.fn, 'id', {

get: function () { return this.attr("id"); },

set: function (newValue) { this.attr("id", newValue); }

});

有趣的是,如果element是一个 DOM 元素,那么:

element.id === $(element).id; // Is true!

于 2015-11-25T17:47:37.480 回答

This answer is useful

2

由于id是一个属性,所以可以通过attr方法获取。

于 2017-11-14T17:27:05.443 回答

This answer is useful

0

这可以是元素 id 、 class 或自动使用 even

------------------------

$(this).attr('id');

=========================

------------------------

$("a.remove[data-id='2']").attr('id');

=========================

------------------------

$("#abc1'").attr('id');

=========================

于 2015-08-03T16:29:34.640 回答

This answer is useful

0

这将最终解决您的问题:

假设您在一个页面上有许多按钮,并且您想根据它们的 ID 使用 jQuery Ajax(或不是 ajax)更改其中一个按钮。

还可以说您有许多不同类型的按钮(用于表单、用于批准和类似目的),并且您希望 jQuery 只处理“喜欢”按钮。

这是一个有效的代码:jQuery 将只处理属于 .cls-hlpb 类的按钮,它将获取被点击按钮的 id,并根据来自 ajax 的数据更改它。



代码取自 w3schools 并进行了更改。

于 2015-08-19T09:11:19.707 回答

This answer is useful

-1

重要提示:如果您使用 jQuery 创建新对象并绑定事件,则必须使用prop而不是attr,如下所示:

$("

",{ id: "yourId", class: "yourClass", html: "" }).on("click", function(e) { alert($(this).prop("id")); }).appendTo("#something");

于 2015-07-15T18:44:02.353 回答

This answer is useful

-1

// include Database connection file

include("db_connection.php");

// Design initial table header

$data = '

';

$query = "SELECT * FROM users";

if (!$result = mysqli_query($con, $query)) {

exit(mysqli_error($con));

}

// if query results contains rows then featch those rows

if(mysqli_num_rows($result) > 0)

{

$number = 1;

while($row = mysqli_fetch_assoc($result))

{

$data .= '

';

$number++;

}

}

else

{

// records now found

$data .= '

';

}

$data .= '

No. First Name Last Name Email Address Update Delete
'.$number.' '.$row['first_name'].' '.$row['last_name'].' '.$row['email'].'

Records not found!
';

echo $data;

?>