简体   繁体   中英

Query document string

I am trying to unwrap the contents of an html document string. I'm being given back a full html document from the froala rich text editor, that includes a head, title, and body tag which I want to remove.

The problem is that any selector I try gives me back an array that has a HTMLTitleElement, and then an array item for each root element in the document contents.

So, for example:

$('<!DOCTYPE html><html><head><title></title></head><body><h2>Summary</h2><div>Some content here</div></body></html>');

gives me back an array that has 3 elements: HTMLTitleElement, HTMLHeadingElement, HTMLDivElement.

I was hoping for a single HTMLBodyElement, or something that I could call .html() on, in order to get the content entered into the editor without all the other html document wrappers.

You can use regular expression to get body content.

var htmlContent = "<!DOCTYPE html><html><head><title></title></head><body><h2>Summary</h2><div>Some content here</div></body></html>";

var bodyContent= /<body.*?>([\s\S]*)<\/body>/.exec(htmlContent)[1];

对于我的特定情况,我发现这满足了我的需求:

$('body', $('#editor-container iframe').contents()).html()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM