admin管理员组文章数量:1295729
I made a simple copy-paste ponent with regular html/css/js. I've tried to turn it into a web ponent and can no longer get the copy-paste behaviour to work.
The markup inside the shadow DOM is basically
<span contenteditable="true">
<slot></slot>
</span>
<button>Copy</button>
The Javascript:
class CopyPaste extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(copyPasteTemplate.content.cloneNode(true));
}
connectedCallback() {
let copyButton = this.shadowRoot.querySelector('button');
let textToCopy = this.shadowRoot.querySelector('span');
function selectElementContents(el) {
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
function copyText() {
selectElementContents(textToCopy);
document.execCommand('copy');
}
copyButton.addEventListener('click', copyText);
textToCopy.addEventListener('click', copyText);
}
}
window.customElements.define('copy-paste', CopyPaste);
I made a simple copy-paste ponent with regular html/css/js. I've tried to turn it into a web ponent and can no longer get the copy-paste behaviour to work.
The markup inside the shadow DOM is basically
<span contenteditable="true">
<slot></slot>
</span>
<button>Copy</button>
The Javascript:
class CopyPaste extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.appendChild(copyPasteTemplate.content.cloneNode(true));
}
connectedCallback() {
let copyButton = this.shadowRoot.querySelector('button');
let textToCopy = this.shadowRoot.querySelector('span');
function selectElementContents(el) {
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
function copyText() {
selectElementContents(textToCopy);
document.execCommand('copy');
}
copyButton.addEventListener('click', copyText);
textToCopy.addEventListener('click', copyText);
}
}
window.customElements.define('copy-paste', CopyPaste);
Share
edited May 13, 2018 at 2:40
Intervalia
11k2 gold badges34 silver badges70 bronze badges
asked May 12, 2018 at 22:40
Ollie WilliamsOllie Williams
2,5444 gold badges29 silver badges50 bronze badges
0
1 Answer
Reset to default 11In your example, the textToCopy
variable refers to the <slot>
element, with no text inside.
If you want to get the ditributed node form the light DOM, of the <copy-paste>
element, you should use the assignedNodes()
method of the <slot>
element:
let textToCopy = this.shadowRoot.querySelector('slot').assignedNodes()[0];
PS: note that the contenteditable
attribute may not work as you expect in your given example.
本文标签: javascriptSelecting slot text in Custom Element Shadow DOMStack Overflow
版权声明:本文标题:javascript - Selecting slot text in Custom Element Shadow DOM - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741624816a2389022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论