我正在尝试使用我的影子根并查看它是否包含应该读取为 false 的 H1。但由于某种原因,我收到了这个错误:
Uncaught TypeError: Failed to execute contains on Node: parameter 1 is not of type Node.
我哪里出错了?
http://plnkr.co/edit/4kgfy05cXuZ9Jefjx01w?p=preview
var root = document.querySelector('div').createShadowRoot();
root.innerHTML = '<content select="h1"></content>';
//console.log(document.querySelector('h1'));
//console.log(document.querySelector('content h1'));
console.log(root.querySelector('content').contains('h1'));
<h1>Header defined in the light DOM</h1>
最佳答案
'h1'
是一个字符串,而不是一个节点。你需要传入一个对象!创建一个 h1
、document.createElement("h1");
并以这种方式传递它。
示例:
使用 var derp = document.createElement("h1"); 缓存变量
或:
console.log(root.querySelector('content').contains(document.createElement('h1'))); //contains(element);
关于javascript - 无法使用 Shadow Root 执行 Contains,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33903118/