简体   繁体   English

Javascript - 从Javascript对象中提取对象ID

[英]Javascript - extract object ID from a Javascript object

So I am not sure if my title is clear enough. 所以我不确定我的头衔是否足够明确。 I essentially have a div saved as a Javascript object which looks like this: [div#field_30.checkbox_group] 我基本上有一个div保存为Javascript对象,如下所示: [div#field_30.checkbox_group]

The field_30 is the ID which I am trying to extract here. field_30是我想在这里提取的ID。 doing something like object.id is not working. 做像object.id这样的事情是行不通的。 Does anyone know how to get the ID? 有谁知道如何获取ID?

Note: I saved the object like this: var object = $(".workspace .selected"); 注意:我保存了这样的对象: var object = $(".workspace .selected"); which grabs the currently selected div inside the object called workspace. 它抓取名为workspace的对象中当前选定的div。 Sorry is this is a rookie mistake, I just can't seem to find anything anywhere. 对不起,这是一个菜鸟的错误,我似乎无处可寻。 Thanks for the help... 谢谢您的帮助...

var object = $(".workspace .selected"); will return a jQuery wrapped element that has jQuery properties and methods rather than element properties and methods. 将返回一个jQuery包装元素,它具有jQuery属性和方法,而不是元素属性和方法。 This means that any of 这意味着任何一个

object[0].id
object.prop("id")
object.attr("id")

should work, but the 1st option should be the best performance-wise. 应该工作,但第一个选项应该是最好的性能。 It gets the id property of the the 1st element contained by the jQuery object, which is your div. 它获取jQuery对象包含的第一个元素的id属性,这是你的div。

Your object is in fact a jQuery object, not a dom object. 您的对象实际上是一个jQuery对象,而不是一个dom对象。

To use the dom object use, 要使用dom对象,

object[0].id

Or using, jquery, (Since it is already there) 或者使用,jquery,(因为它已经存在)

object.prop('id'); 

You can use either $jquery_object.attr('id') or $jquery_object.eq(0).id 您可以使用$jquery_object.attr('id')$jquery_object.eq(0).id

See this for exemple: http://jsfiddle.net/cquuT/ 请参阅此示例: http//jsfiddle.net/cquuT/

In this case it looks like object is the result of a jQuery select. 在这种情况下,它看起来就像object是一个结果jQuery选择。 To get to the actual DOM object you need to use [0] . 要获得实际的DOM对象,您需要使用[0] Then you can access the id property 然后您可以访问id属性

object[0].id

I don't see a complete answer here, so I'll provide my own. 我在这里看不到完整的答案,所以我会提供自己的答案。

If you're using jQuery selector $() , then you'll get jQuery-wrapped collection , not a single element. 如果你正在使用jQuery选择器$() ,那么你将获得jQuery包装的集合 ,而不是单个元素。

(I assume now that you're using jQuery 1.5.2, the same as StackOverflow uses now.) (我现在假设您使用的是jQuery 1.5.2,与StackOverflow现在使用的相同。)

  • Universal solution to get id s of all elements returned by selector is: 获取选择器返回的所有元素的id的通用解决方案是:

     .map(function(){ return this.id; }) 

    Running $(".post-text").map(function(){ return this.id; }) on current page will return something like: ["", "", "", "", ""] 在当前页面上运行$(".post-text").map(function(){ return this.id; })将返回如下内容: ["", "", "", "", ""]

  • To get id of the first element returned by selector use: 要获取由selector使用返回的第一个元素的id

     .attr('id') 

    Running $("div").attr('id') on current page will return "notify-container" . 在当前页面上运行$("div").attr('id')将返回"notify-container"

    Since jQuery 1.6 you can also use .prop('id') here. 从jQuery 1.6开始,你也可以在这里使用.prop('id')

If you know, that query will return only one element or you just want the first element matching given selector, then use .attr which is obviously a simpler solution. 如果你知道,那个查询只返回一个元素,或者你只想让第一个元素匹配给定的选择器,那么使用.attr这显然是一个更简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM