简体   繁体   中英

Get the id of an element by position using javascript?

Using javascript, is it possible to get the id of an element by its spatial position in the document using javascript?

Don't think its possible. But just wondering if it is.

var x = 100, y = 100;
var id = document.elementFromPoint(x, y).id;

Browser support.

This is crazy. We have an answer so far using jQuery .attr() and one using javascript plain .getAttribute() . While both will work, the most efficient way to get the id of a given element is simply:

element.id

It's a property of the object and there's no reason not to just access it as a javascript property.

You will, of course, have to obtain the DOM element itself before you can get the id. You haven't shown us any HTML or told us how you access the element so we can't help with specifics there at all. If you had a link and a click handler, you could get the id like this:

<a id="test" href="#" onclick="clickme(this)">Click Here</a>

function clickme(link) {
    var id = link.id;
}

Yes you can get the id of the element.

Using jquery u can get the id of the element

var id =$("element selector").attr("id");

see this exmaple

http://jsfiddle.net/6nsz6/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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