开发者

Creating a document from a string in Gecko

开发者 https://www.devze.com 2022-12-09 13:41 出处:网络
I have a string such as <html><body><div id=\"message\">Hello World!</div></body></html>

I have a string such as <html><body><div id="message">Hello World!</div></body></html> and I would like to get the content of the #message element without parsing the HTML myself.

I thought maybe I could create a document object from a string in Gecko (this is for a Firefox add on) but I don't see any simple way.

I noticed that there is a createDocument method, but that doesn't take a string. I'd have to strip the <html> portion from the text, and then again I'm starting to assume stuff.

Anyone have any ideas? Thanks.

EDIT: This seems to work for me:

doc = document.implementation.createDocument( "http://www.w3.org/1999/xhtml", "html", null开发者_StackOverflow社区 );
doc.firstChild.innerHTML = '<html><body><div id="message">Hello World!</div></body></html>';
node = doc.getElementById( "message" ); 
alert( node.innerHTML );


Don't like answering my own question, but this seems to have worked for me:

doc = document.implementation.createDocument( "http://www.w3.org/1999/xhtml", "html", null );
doc.firstChild.innerHTML = '<html><body><div id="message">Hello World!</div></body></html>';
node = doc.getElementById( "message" ); 
alert( node.innerHTML );


Where do you get the string from? If it's XML, you could get away with using DOMParser.

Otherwise, you have to create an HTML document - https://developer.mozilla.org/en/Parsing_HTML_From_Chrome.

The fact that just using createDocument works seems suspicious, because people used more complicated solutions all this time.


Is it always that syntax? If so, why not use a regex?

0

精彩评论

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