假设我正在编写以下 Web 组件或自定义元素:
<!doctype html>
<html>
<head>
<script src="js/MutationObserver.js"></script>
<script src="js/CustomElements.js"></script>
<script>
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() { console.log('created');};
proto.enteredViewCallback = function() {};
document.register('x-foo', {prototype: proto});
document.createElement('x-foo');
</script>
</head>
<body>
</body>
</html>
( polyfill on github ) 这适用于 Chrome。但是当我尝试在 PhantomJS 中运行它时,出现以下错误:
TypeError: instanceof called on an object with an invalid prototype property.
http://localhost/~me/js/CustomElements.js:18
http://localhost/~me/js/CustomElements.js:217 in instantiate
http://localhost/~me/js/CustomElements.js:333
http://localhost/~me/js/CustomElements.js:342 in createElement
...
如果要复现,这里是我的phantomjs脚本
var page = require('webpage').create();
page.open('http://localhost/~me/test.html', function() {
phantom.exit();
});
我已经尝试过其他的 polyfills
但实际上它们都不起作用。任何想法如何解决这个问题?
最佳答案
我遇到了同样的问题。它是用这个 polyfill 解决的: https://github.com/WebReflection/document-register-element
您需要使用 document.registerElement 方法。
关于javascript - 在 phantomjs 中没有用于 document.register 的 polyfill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20668488/