我想制作按钮,它将选择我的 p 标签中的所有文本。标签示例:
<p class="foo">
some text
<div>this div is child of p</div>
<div>one more row</div>
</p>
Run Code Online (Sandbox Code Playgroud)
解决方案也必须通过 window.getSelection() 或类似但不是 jQuery。我试图通过使用 selectNodeContents(el) 来做到这一点,但结果只是第一行。
任何人都可以帮助我,因为我无法弄清楚这一点。
编辑代码:
var selection = window.getSelection();
var txt = document.getElementsByClassName("foo");
var range = document.createRange();
range.selectNodeContents(txt);
selection.removeAllRanges();
selection.addRange(range);
Run Code Online (Sandbox Code Playgroud)