Given a node in an HTML document, N1, I need a concise way of finding the closest node to N1, say N2, with some tagName (e.g. to get the nearest link). I will be injecting the js code to do this into a remote browser (using Selenium) and so need it to be as concise as possible. I would otherwise write a recursive functio开发者_运维问答n that takes N1 and visits all siblings, then moves up and down the DOM tree until it finds a match.
Is there is a shortcut that doesn't rely on 3rd party libraries?
I don't think there's a more concise way than recursively searching the tree, no. It seems pretty concise to me. I don't think there are any shortcuts, either.
Try this:
http://v3.thewatchmakerproject.com/journal/329/finding-html-elements-using-javascript-nextsibling-and-previoussibling
精彩评论