开发者

Find the nearest named anchor to an element on a web page in JavaScript

开发者 https://www.devze.com 2023-01-18 05:26 出处:网络
I\'d like to be able to right-click on an element on a web page and get the URL of the nearest anchor.

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.

  1. Use jQuerify or similar to activate jQuery on the page.
  2. Then use the console of Chrome, Safari or Firefox to enter your code.
  3. Code could begin along these lines: $('body').live('click', functionThatFindsClosestAnchorElement);

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消