如何使用 jQuery 获取元素的 ID?
<div id="test"></div>
<script>
$(document).ready(function() {
alert($('#test').id);
});
</script>
为什么上面的方法不起作用,我应该怎么做?
<div id="test"></div>
<script>
$(document).ready(function() {
alert($('#test').id);
});
</script>
Why doesn't the above work, and how should I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
jQuery 方式:
在您的示例中:
或者通过 DOM:
甚至 :
以及在 JQuery 中使用
$('#test').get(0)
甚至$('#test')[0]< 的原因/code> 是
$('#test')
是一个 JQuery 选择器,并且返回结果的 array(),其默认功能不是单个元素,而是DOM 的替代方案jquery 中的选择器
与
.attr()
不同,$('#test').prop('foo')
抓取指定的 DOMfoo< /code> 属性,而
$('#test').attr('foo')
获取指定的 HTMLfoo
属性,您可以找到有关差异的更多详细信息此处。The jQuery way:
In your example:
Or through the DOM:
or even :
and reason behind usage of
$('#test').get(0)
in JQuery or even$('#test')[0]
is that$('#test')
is a JQuery selector and returns an array() of results not a single element by its default functionalityan alternative for DOM selector in jquery is
which is different from
.attr()
and$('#test').prop('foo')
grabs the specified DOMfoo
property, while$('#test').attr('foo')
grabs the specified HTMLfoo
attribute and you can find more details about differences here.$('selector').attr('id')
将返回第一个匹配元素的 id。 参考。如果您的匹配集包含多个元素,您可以使用传统的
.each
迭代器< /a> 返回一个包含每个 id 的数组:或者,如果您愿意变得更加坚韧,您可以避免使用包装器并使用
.map
快捷方式。$('selector').attr('id')
will return the id of the first matched element. Reference.If your matched set contains more than one element, you can use the conventional
.each
iterator to return an array containing each of the ids:Or, if you're willing to get a little grittier, you can avoid the wrapper and use the
.map
shortcut.id
是 htmlElement
的属性。但是,当您编写$("#something")
时,它会返回一个包装匹配 DOM 元素的 jQuery 对象。要获取第一个匹配的 DOM 元素,请调用get(0)
在此本机元素上,您可以调用 id 或任何其他本机 DOM 属性或函数。
这就是
id
在您的代码中不起作用的原因。或者,使用 jQuery 的
attr
方法,正如其他答案建议的那样,获取第一个匹配元素的id
属性。id
is a property of an htmlElement
. However, when you write$("#something")
, it returns a jQuery object that wraps the matching DOM element(s). To get the first matching DOM element back, callget(0)
On this native element, you can call id, or any other native DOM property or function.
That's the reason why
id
isn't working in your code.Alternatively, use jQuery's
attr
method as other answers suggest to get theid
attribute of the first matching element.上面的答案很好,但是随着jquery的发展..所以你也可以这样做:
Above answers are great, but as jquery evolves.. so you can also do:
当然需要一些检查代码,但很容易实现!
Some checking code required of course, but easily implemented!
如果您想获取某个元素的 ID(假设通过类选择器),当在该特定元素上触发事件(在本例中为单击事件)时,则以下代码将完成这项工作:
If you want to get an ID of an element, let's say by a class selector, when an event (in this case click event) was fired on that specific element, then the following will do the job:
.id
不是有效的 jquery 函数。您需要使用.attr()
函数来访问元素拥有的属性。您可以使用.attr()
通过指定两个参数来更改属性值,或通过指定一个参数来获取值。http://api.jquery.com/attr/
.id
is not a valid jquery function. You need to use the.attr()
function to access attributes an element possesses. You can use.attr()
to both change an attribute value by specifying two parameters, or get the value by specifying one.http://api.jquery.com/attr/
$('#test').attr('id')
在你的例子中:
$('#test').attr('id')
In your example:
好吧,似乎还没有解决方案,我想提出我自己的解决方案,即 JQuery 原型的扩展。我将其放入在 JQuery 库之后加载的帮助程序文件中,因此对
window.jQuery
的检查可能并不完美,但它可能是包含到 JQuery 库中的一个开始。
返回单个字符串值或字符串值数组。字符串值数组用于使用多元素选择器的事件。
Well, seems there has not been a solution and would like to propose my own solution that is an expansion of the JQuery prototype's. I put this in a Helper file that is loaded after the JQuery library, hence the check for
window.jQuery
It may not be perfect but it is a start to maybe getting inclusion into the JQuery library.
Returns either a single string value or an Array of string values. The Array of string values, is for the event an multi-element selector was used.
$('#test')
返回一个 jQuery 对象,因此您不能简单地使用object.id
来获取您需要的
Id
使用$('#test').attr('id')
,它返回您所需的元素ID
这也可以按如下方式完成,
$ ('#test').get(0).id
等于document.getElementById('test').id
$('#test')
returns a jQuery object, so you can't use simplyobject.id
to get itsId
you need to use
$('#test').attr('id')
, which returns your requiredID
of the elementThis can also be done as follows ,
$('#test').get(0).id
which is equal todocument.getElementById('test').id
使用上面的代码你可以得到id。
Using above code you can get id.
也许对找到此线程的其他人有用。仅当您已经使用 jQuery 时,下面的代码才有效。该函数始终返回一个标识符。如果元素没有标识符,则该函数会生成标识符并将其附加到元素。
使用方法:
Maybe useful for others that find this thread. The code below will only work if you already use jQuery. The function returns always an identifier. If the element doesn't have an identifier the function generates the identifier and append this to the element.
How to use:
这是一个老问题,但是从 2015 年开始这可能确实有效:
并且您还可以进行分配:
只要您定义以下 JQuery 插件:
有趣的是,如果
element
是一个 DOM 元素,那么:This is an old question, but as of 2015 this may actually work:
And you can also make assignments:
As long as you define the following JQuery plugin:
Interestingly, if
element
is a DOM element, then:由于 id 是一个属性,因此您可以使用
attr
方法获取它。Since the id is an attribute, you can get it by using the
attr
method.这可以是元素 id 、 class ,或者自动使用 Even
This can be element id , class , or automatically using even
这将最终解决您的问题:
假设您在页面上有许多按钮,并且您想根据按钮的 ID 使用 jQuery Ajax(或不使用 ajax)更改其中一个按钮。
还可以说您有许多不同类型的按钮(用于表单、用于批准和用于类似目的),并且您希望 jQuery 仅处理“喜欢”按钮。
这是一个正在运行的代码:
jQuery 将仅处理 .cls-hlpb 类的按钮,
它将获取被单击按钮的 id
并会根据来自ajax的数据改变它。
代码取自 w3schools 并进行了更改。
This will finally solve your problems:
lets say you have many buttons on a page and you want to change one of them with jQuery Ajax (or not ajax) depending on their ID.
lets also say that you have many different type of buttons (for forms, for approval and for like purposes), and you want the jQuery to treat only the "like" buttons.
here is a code that is working:
the jQuery will treat only the buttons that are of class .cls-hlpb,
it will take the id of the button that was clicked
and will change it according to the data that comes from the ajax.
code was taken from w3schools and changed.
它不回答OP,但其他人可能会感兴趣:在这种情况下您可以访问
.id
字段:it does not answer the OP, but may be interesting to others: you can access the
.id
field in this case:重要提示:如果您使用 jQuery 创建新对象并绑定事件,则必须使用 prop 而不是 attr,如下所示:
$("
",{ id: "yourId", class: "yourClass", html: "" }).on("click", function(e ) {alert($(this).prop("id")); }).appendTo("#something");
Important: if you are creating a new object with jQuery and binding an event, you MUST use prop and not attr, like this:
$("<div/>",{ id: "yourId", class: "yourClass", html: "<span></span>" }).on("click", function(e) { alert($(this).prop("id")); }).appendTo("#something");