如何在文本字符串上执行document.querySelectorAll而不将其插入DOM [英] How to execute document.querySelectorAll on a text string without inserting it into DOM

查看:102
本文介绍了如何在文本字符串上执行document.querySelectorAll而不将其插入DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var html = '<p>sup</p>'

我想对该文本运行 document.querySelectorAll('p')而不将其插入dom。

I want to run document.querySelectorAll('p') on that text without inserting it into the dom.

在jQuery中您可以执行 $(html).find('p')

In jQuery you can do $(html).find('p')

如果这是不可能的,做一个临时插入物以确保它不会干扰任何东西的最干净的方法是什么。然后只查询该元素。然后将其删除。

If it's not possible, what's the cleanest way to to do a temporary insert making sure it doesn't interfere with anything. then only query that element. then remove it.

(我正在执行ajax请求并尝试解析返回的html)

(I'm doing ajax requests and trying to parse the returned html)

推荐答案

在IE 10及更高版本中,您可以使用 DOM分析器对象

With IE 10 and above, you can use the DOM Parser object to parse DOM directly from HTML.

var parser = new DOMParser();
var doc = parser.parseFromString(html, "text/html");
var paragraphs = doc.querySelectorAll('p');

这篇关于如何在文本字符串上执行document.querySelectorAll而不将其插入DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆