我有这样的代码:
function select_all(element) {
var range;
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
var selection = window.getSelection();
if (selection.setBaseAndExtent) {
selection.setBaseAndExtent(element, 0, element, 1);
} else if (document.createRange) {
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
}
它运行正常,除了一件事,我有这样的HTML:
<div class="root">
<div class="wrapper">
<div class="output" role="log">
<div>text</div>
<!-- there can be more divs here -->
</div>
<div class="inner" style="width: 100%;">
<span class="foo" style="visibility: visible;">
<span>> </span>
</span>
<div class="cursor-line" role="presentation" aria-hidden="true">
<span></span>
<span class="cursor"> </span>
<span></span>
</div>
<!-- there can be more divs here -->
<textarea autocapitalize="off" spellcheck="false" tabindex="1" class="clipboard" style=""></textarea>
</div>
</div>
</div>
当我调用该函数textarea也被选中时,我试图在textarea中清除选择,但似乎只有一个选择而且它消失了。
我还尝试在选择之前删除textarea并在之后追加,但这也会选择textarea。
如何选择除textarea之外的root?
我无法将textarea移到外面因为需要相对于.inner