Вопрос по javascript, dom, html – Разобрать строку HTML с помощью JS

200

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

var parser = new HTMLtoDOM("<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>", document);

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededJavaScript DOMParser access innerHTML and other properties Rob W
Error: User Rate Limit Exceededdoc.getElementsByTagName('a')Error: User Rate Limit Exceededdoc.links). Rob W
Error: User Rate Limit Exceededstackoverflow.com/questions/23616226/… Mike Lyons

Ваш Ответ

8   ответов
5

var range = document.createRange();
range.selectNode(document.body); // required in Safari
var fragment = range.createContextualFragment('<h1>html...</h1>');
var firstNode = fragment.firstChild;

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededinnerHTMLError: User Rate Limit Exceeded<img>Error: User Rate Limit Exceededonerror.
6
var $doc = new DOMParser().parseFromString($html, "text/html");
$As = $('a', $doc);
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededDOMParserError: User Rate Limit Exceededtext/htmlError: User Rate Limit Exceededthis MDN pageError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded$Error: User Rate Limit Exceededlinked duplicate, text/htmlError: User Rate Limit Exceeded
13

<td>Test</td>. This one won't work on the div.innerHTML solution nor DOMParser.prototype.parseFromString nor range.createContextualFragment solution. The td tag goes missing and only the text remains.

function parseHTML(html) {
    var t = document.createElement('template');
    t.innerHTML = html;
    return t.content.cloneNode(true);
}

var documentFragment = parseHTML('<td>Test</td>');

Error: User Rate Limit Exceededpolyfill the <template> tagError: User Rate Limit ExceededpolyfillError: User Rate Limit Exceededwebcomponents.jsError: User Rate Limit Exceeded
311

var el = document.createElement( 'html' );
el.innerHTML = "<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>";

el.getElementsByTagName( 'a' ); // Live NodeList of your anchor elements

var el = $( '<div></div>' );
el.html("<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>");

$('a', el) // All the anchor elements
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededanchorError: User Rate Limit Exceeded
Error: User Rate Limit Exceeded stage
Error: User Rate Limit Exceeded stage
Error: User Rate Limit Exceededdocument.createElement('html');Error: User Rate Limit Exceeded<head>Error: User Rate Limit Exceeded<body>Error: User Rate Limit Exceeded
1
const parse = Range.prototype.createContextualFragment.bind(document.createRange());

document.body.appendChild( parse('<p><strong>Today is:</strong></p>') ),
document.body.appendChild( parse(`<p style="background: #eee">${new Date()}</p>`) );
6

parseHTML

a Document when your file starts with a doctype.

a DocumentFragment when your file doesn't start with a doctype.

The code :
function parseHTML(markup) {
    if (markup.toLowerCase().trim().indexOf('<!doctype') === 0) {
        var doc = document.implementation.createHTMLDocument("");
        doc.documentElement.innerHTML = markup;
        return doc;
    } else if ('content' in document.createElement('template')) {
       // Template tag exists!
       var el = document.createElement('template');
       el.innerHTML = markup;
       return el.content;
    } else {
       // Template tag doesn't exist!
       var docfrag = document.createDocumentFragment();
       var el = document.createElement('body');
       el.innerHTML = markup;
       for (i = 0; 0 < el.childNodes.length;) {
           docfrag.appendChild(el.childNodes[i]);
       }
       return docfrag;
    }
}
How to use :
var links = parseHTML('<!doctype html><html><head></head><body><a>Link 1</a><a>Link 2</a></body></html>').getElementsByTagName('a');
Error: User Rate Limit ExceededtrimError: User Rate Limit Exceededstackoverflow.com/q/2308134/3210837.
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
2

var html = "<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>";
var anchors = $('<div/>').append(html).find('a').get();

Error: User Rate Limit Exceeded
162

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(txt, 'text/html');
// do whatever you want with htmlDoc.getElementsByTagName('a');

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(txt, 'text/xml');
// do whatever you want with htmlDoc.getElementsByTagName('a');

Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededdocumentURLError: User Rate Limit ExceededwindowError: User Rate Limit Exceeded
Error: User Rate Limit Exceededcaniuse.com/#feat=xml-serializer
Error: User Rate Limit Exceededparse()Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededonlyError: User Rate Limit Exceedednew DOMParserError: User Rate Limit Exceeded

Похожие вопросы