admin管理员组文章数量:1379636
Here I have created elements with shadow dom.
/* some preparing code */
this.createShadowRoot(); // creates shadow root, this refers to element
Later in the code I would access the shadow dom that I have created. These work:
this.shadowRoot.getElementById("...")
this.shadowRoot.querySelector("...")
However this does not:
$("...", this.shadowRoot)
Why is that? As a temporary solution this works at the moment:
$("...", this.shadowRoot.children)
Is there a better/more elegant way to work with the shadow root using jQuery?
Note that the version of jQuery I'm using is 2.1.1 and I'm only dealing with Chrome.
Edit: Apparently this.shadowRoot.children
doesn't work when finding nodes at the top level.
Here I have created elements with shadow dom.
/* some preparing code */
this.createShadowRoot(); // creates shadow root, this refers to element
Later in the code I would access the shadow dom that I have created. These work:
this.shadowRoot.getElementById("...")
this.shadowRoot.querySelector("...")
However this does not:
$("...", this.shadowRoot)
Why is that? As a temporary solution this works at the moment:
$("...", this.shadowRoot.children)
Is there a better/more elegant way to work with the shadow root using jQuery?
Note that the version of jQuery I'm using is 2.1.1 and I'm only dealing with Chrome.
Edit: Apparently this.shadowRoot.children
doesn't work when finding nodes at the top level.
1 Answer
Reset to default 5This might be a problem with jQuery 2.1.1.
Using jQuery 2.1.3 in jsfiddle seems to solve this problem:
https://jsfiddle/bnh74s87/
document.addEventListener("DOMContentLoaded",function(){
var div=document.getElementById("dTest");
var shadow=div.createShadowRoot();
shadow.innerHTML='<p>Hi!</p>';
document.body.appendChild(document.createTextNode(shadow.childNodes.length));
console.log(shadow.querySelectorAll("p"));
console.log($("p",shadow));
$("p",shadow).html("Hello!");
},false);
<script src="https://code.jquery./jquery-2.1.3.js"></script>
<div id="dTest"></div>
本文标签: javascriptUsing jQuery with shadow domStack Overflow
版权声明:本文标题:javascript - Using jQuery with shadow dom - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744480278a2608126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论