javascript : how to find the parent node given the innerhtml
Here is my HTML snippet.
<div id='advanced'>
<a id='javscript'>testing</a&g开发者_如何转开发t;
</div>
I need to find the parent node given the innerhtml testing.
As others have said you may want to consider re-thinking whatever it is you're trying to achieve. Anyway, if you need to do just one lookup then you just need to iterate over each node in the DOM tree and test your string against the text content. If you need to do it multiple times then this will get very slow. So a possible solution is this: loop over the DOM and put the innerHTML into a hash, with the text as the key (field) and the parent node as the value. Then for any text you can test against the hash, returning the parent node if it exists. But - don't just bang in every innerHTML for every node - this will result in a large object. If it's just text nodes you're after then only stick these into your hash.
You might want to consider going with a library, specifically jQuery. It would make this type of search very easy for you. This should work: http://api.jquery.com/contains-selector/
精彩评论