문제

I like to access an anchor (a) element with a specific name in jQuery to obtain its position. I tried the following:

HTML:

<a name="test">An anchor.</a>

JavaScript:

var top = $("a [name=test]").position().top;

Returns empty object.

var top = $("a").position().top;

and

var top = $("[name=test]").position().top;

finds it. How do I write it to get an anchor element with name "test"?

도움이 되었습니까?

해결책

You don't need space after a in your selector:

var top = $("a[name=test]").position().top;

If you put space then it'll find the child elements with name test of any anchor instead.

다른 팁

Remove the space :

var top = $("a[name=test]").position().top;

A space indicates that you're looking for a child element.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top