Find the nearest named anchor to an element on a web page in JavaScript
I'd like to be able to right-click on an element on a web page and get the URL of the nearest anchor.
For example, say I'm looking at online documentation, and I see a paragraph saying something surprising. Then I'd like to be able to right-click on that, choose "copy URL of nearest anchor", go to my IDE, and be able to paste a URL, like "http://docs.somelang.org/1.9/manual.htm#section9" into a code comment.
Currently, I have to do "view source" and hunt around for the nearest 开发者_Go百科element.
Anything which does something reasonably close to this in any major browser would suffice, but my ideal answer would be something which can be added to the right-click menu in Chrome.
There's a bookmarklet here which does this: Named anchors
You could write a jQuery script that you run on each page, triggering on clicks.
- Use jQuerify or similar to activate jQuery on the page.
- Then use the console of Chrome, Safari or Firefox to enter your code.
- Code could begin along these lines: $('body').live('click', functionThatFindsClosestAnchorElement);
Hope this helps!
精彩评论