如何使用 jQuery 查找元素的 id

发布于 2024-10-19 04:11:43 字数 322 浏览 0 评论 0原文

这是我遍历父母的html代码

  < body >    
     < div id="map" >

,直到找到主体

var pageFrom= $(theElement).parentsUntil( 'body' );    
var pageFromId = $(pageFrom).attr("id"); 

在firebug中找到地图我看到pageFrom [div#map.current]

问题是pageFromId是“”,它应该是“map”

This is the html code

  < body >    
     < div id="map" >

I am traversing the parents until I find the body to find the map

var pageFrom= $(theElement).parentsUntil( 'body' );    
var pageFromId = $(pageFrom).attr("id"); 

in firebug i see pageFrom [div#map.current]

The problem is that pageFromId is "" and it should be "map"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦幻之岛 2024-10-26 04:11:43

如果您使用的是 parentsUntil()[docs]< /i> 方法,它正在重新调整祖先的集合,从距离起点最近的到距离最远的。

因此,您需要访问集合中的最后一个来获取其 ID。

var pageFrom= $(theElement).parentsUntil( 'body' );
var pageFromId = pageFrom.last().attr('id');

var pageFrom= $(theElement).parentsUntil( 'body' );
var pageFromId = pageFrom.slice(-1).attr('id');

请注意,您还可以获取 ID 作为元素的属性:

var pageFromId = pageFrom.get(-1).id;

If you're using the parentsUntil()[docs] method, it is retuning a collection of ancestors ordered from those nearest the starting point to those farthest away.

Because of this, you'll need to access the last one in the collection to get its ID.

var pageFrom= $(theElement).parentsUntil( 'body' );
var pageFromId = pageFrom.last().attr('id');

or

var pageFrom= $(theElement).parentsUntil( 'body' );
var pageFromId = pageFrom.slice(-1).attr('id');

Note that you can also grab the ID as a property on the element:

var pageFromId = pageFrom.get(-1).id;
神仙妹妹 2024-10-26 04:11:43

试试这个:

$(theElement).closest( 'map' );
or
$(theElement).closest( 'body' ).attr('id'); 

try this:

$(theElement).closest( 'map' );
or
$(theElement).closest( 'body' ).attr('id'); 
混吃等死 2024-10-26 04:11:43

或许

pageFrom.first().attr("id")

但你应该更具体地说明你需要什么。你真的需要元素和主体之间的所有父母吗?否则,您可以使用.closest。

另外,在您的初始代码中,您不需要使用 $(pageFrom) 因为它已经是一个 jquery 对象。

Maybe

pageFrom.first().attr("id")

?

But you should be more specific about what you need. Do you really need all the parents between the element and body? Otherwise, you can use .closest.

Also, in your inital code, you don't need to use $(pageFrom) since it's already a jquery object.

奶气 2024-10-26 04:11:43

我不太清楚你在这里问什么。但是,如果这里的模式是 之后的第一个

有一个 ID,而这就是您想要获取的内容,那么简单因为这应该可以解决问题:

$('body').find('div:first').attr('id');

没有理由向上遍历到文档的主体——应该总是有一个,所以为什么不从那里开始并向下工作呢?

I'm not quite sure what you're asking here. But if the pattern here is that the first <div> after the <body> has an ID, and that's what you're trying to grab, something as simple as this should do the trick:

$('body').find('div:first').attr('id');

There's no reason to traverse upward to the document's body -- there should always be one, so why not start there and work down?

两个我 2024-10-26 04:11:43

来自文档:

给定一个 jQuery 对象来表示
一组 DOM 元素,
.parentsUntil()方法遍历
通过这些祖先
元素,直到到达一个元素
与传入的选择器匹配
方法的参数。由此产生的
jQuery 对象包含所有
祖先直到但不包括
与 .parentsUntil() 匹配的一个
选择器。

这意味着您尝试在元素数组上调用 attr,而不是最终元素本身。你到底想做什么?您想查找正文中的第一个元素,还是想查找与选择器匹配的最接近的元素?

// find first element of a type
$('div:first').attr('id')

// find closest element of a type
$(this).closest('div').attr('id')

From the docs:

Given a jQuery object that represents
a set of DOM elements, the
.parentsUntil() method traverses
through the ancestors of these
elements until it reaches an element
matched by the selector passed in the
method's argument. The resulting
jQuery object contains all of the
ancestors up to but not including the
one matched by the .parentsUntil()
selector.

That means you're attempting to call attr on an array of elements, not the final element itself. What exactly is it that you want to do? Do you want to find the first element within the body, or do you want to find the closest element that matches a selector?

// find first element of a type
$('div:first').attr('id')

// find closest element of a type
$(this).closest('div').attr('id')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文