Web worker中的DOM操作 [英] DOM manipulation inside web worker

查看:1323
本文介绍了Web worker中的DOM操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道工作人员不能直接操作文档,但DOM API方法怎么样?他们去哪儿了?!

I know that workers can't manipulate the document directly, but how about the DOM API methods? Where did they go?!

例如,如果我发出一个接收HTML片段的请求,如果只是为了解析它,我应该做什么从特定节点检索一些数据?!

For example if I make a request that receives a HTML fragment, what I'm supposed to do if need just to parse it in order to retrieve some data from a specific node?!

绝对无法在网络工作者上使用虚拟DOM?!

There's absolutely no way to work with virtual DOM on web workers?!

推荐答案

浏览器支持



DOMParser document.implementation 通常用于在浏览器中将HTML解析为DOM。在工作环境中都没有。

Support in browsers

DOMParser or document.implementation are usually used to parse HTML to DOM in browsers. Neither is available in worker context.

在Firefox中,这是不可能的,因为有人刚刚完全延迟并且决定所有线程只有一个DOM解析器实例。请参阅此错误: https://bugzilla.mozilla.org/show_bug.cgi?id=677123

In Firefox, this is not possible because someone just went full retard and decided there will be only one DOM parser instance for all threads. See this bug: https://bugzilla.mozilla.org/show_bug.cgi?id=677123

在谷歌浏览器中它也不起作用。

In google chrome it doesn't work either.

这是正确的,因为浏览器开发人员没有意识到DOM和XML解析将成为WebWorkers的主要用途之一,我们将不得不回到外部库。最好的选择似乎是 JSDOM ,但你需要找出如何浏览它

That's right, since browser developers didn't realize DOM and XML parsing will be one of main uses of WebWorkers, we'll have to fall back to external library. The best bet seems to be JSDOM, but you'll need to figure out how to browserify it.

这是我使用 DOMParser 的失败尝试,我将其保留以用于此主题的未来实验: https://jsfiddle.net/svaqb2wn/2/

Here's my failed attempt with DOMParser, I keep it for future experiments on this topic: https://jsfiddle.net/svaqb2wn/2/

这篇关于Web worker中的DOM操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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