Jquery: get the TOP and LEFT coordinates of the 5th paragraph?
I need to get the TOP and LEFT coordinates of the 5th paragraph on my page, or maybe the 6th paragraph, or the 8th, im just trying to figure out how to get the coordinates for any paragraph on the page 开发者_Go百科I choose. I then need to insert a div 20px to the left of the top left of the paragraph i choose. Can anyone help?
You can use .offset()
for this, for example:
var offset = $("p:eq(4)").offset();
//top is offset.top
//left is offset.left
Or, if you need it to be more dynamic on the "which" paragraph, use .eq()
like this:
$("p").eq(4).offset();
You can test it out here.
精彩评论