我无法在 HTML5 中访问 jQuery网络工作者 https://en.wikipedia.org/wiki/Web_worker。我有办法做到这一点吗?
长话短说:包括这个脚本 http://fel.8u.cz/workerrjs/main/workerFakeDOM.js在 jQuery 之前
JQuery 最初评估了很多document
属性来检查浏览器功能。document
Worker 中没有定义,您实际上无法创建Document
此时网络工作者的实例。 JQuery 还没有为此做好准备 - 正如您在问题的评论中看到的那样,似乎没有人理解在没有 DOM 的情况下您会使用 JQuery 做什么。
因为正如我所说,JQuery 需要document
为了初始化,我创建了一个虚拟的假文档对象。该对象在 JQuery 测试期间充当真实文档:
var document = self.document = {parentNode: null, nodeType: 9, toString: function() {return "FakeDocument"}};
var window = self.window = self;
var fakeElement = Object.create(document);
fakeElement.nodeType = 1;
fakeElement.toString=function() {return "FakeElement"};
fakeElement.parentNode = fakeElement.firstChild = fakeElement.lastChild = fakeElement;
fakeElement.ownerDocument = document;
document.head = document.body = fakeElement;
document.ownerDocument = document.documentElement = document;
document.getElementById = document.createElement = function() {return fakeElement;};
document.createDocumentFragment = function() {return this;};
document.getElementsByTagName = document.getElementsByClassName = function() {return [fakeElement];};
document.getAttribute = document.setAttribute = document.removeChild =
document.addEventListener = document.removeEventListener =
function() {return null;};
document.cloneNode = document.appendChild = function() {return this;};
document.appendChild = function(child) {return child;};
请注意,这个假文档只是为了允许 jQuery 加载,它不允许任何真正的 DOM 操作。
用法示例:
importScripts("workerFakeDOM.js");
importScripts('jquery-2.1.4.min.js');
console.log("JQuery version:", $.fn.jquery);
测试脚本 http://fel.8u.cz/workerrjs/main/jquery.html
允许您使用我的脚本尝试各种版本的 JQuery。
JSfiddle http://jsfiddle.net/b3kf3e55/2/
检查您是否正在使用http://
,我的域名不支持https://
.
下载为脚本 http://fel.8u.cz/workerrjs/main/workerFakeDOM.js
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)