简体   繁体   English

JavaScript 设置 Window 选择

[英]JavaScript Set Window selection

In JavaScript, there is a method window.getSelection() , that lets me get the current selection that the user has made.在 JavaScript 中,有一个方法window.getSelection() ,它可以让我获取用户所做的当前选择。

Is there a corresponding function, something like window.setSelection() , that will let me set, or clear, the current selection?是否有相应的 function 之类的window.setSelection()可以让我设置或清除当前选择?

Clearing the selection in all major browsers:清除所有主要浏览器中的选择:

function clearSelection() {
    if (window.getSelection) {
        window.getSelection().removeAllRanges();
    } else if (document.selection) {
        document.selection.empty();
    }
}

Selecting content requires use of DOM Range and Selection objects in most browsers and TextRange objects in IE < 9. Here's a simple cross-browser example that selects the contents of a particular element:选择内容需要在大多数浏览器中使用DOM RangeSelection对象,在 IE < 9 中使用TextRange对象。下面是一个简单的跨浏览器示例,用于选择特定元素的内容:

function selectElement(element) {
    if (window.getSelection) {
        var sel = window.getSelection();
        sel.removeAllRanges();
        var range = document.createRange();
        range.selectNodeContents(element);
        sel.addRange(range);
    } else if (document.selection) {
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(element);
        textRange.select();
    }
}

Maybe this will do it:也许这会做到:

window.selection.clear();

Crossbrowser version:跨浏览器版本:

if (window.getSelection) {
   if (window.getSelection().empty) {  // Chrome
     window.getSelection().empty();
   } else if (window.getSelection().removeAllRanges) {  // Firefox
     window.getSelection().removeAllRanges();
   }
} else if (document.selection) {  // IE?
  document.selection.empty();
}

In browsers that support the "selection" and "range" stuff, you'll want to create a range object and then set its start/end.在支持“选择”和“范围”内容的浏览器中,您需要创建一个范围 object 然后设置它的开始/结束。 The Mozilla documentation for the "range" object has a lot of information. “范围” object 的Mozilla 文档有很多信息。

Chrome doesn't support this, at least not with that API, and I bet Safari doesn't either. Chrome 不支持这一点,至少 API 不支持,我敢打赌 Safari 也不支持。

edit — thanks to @Tim Down for noting that WebKit (Chrome & Safari) do indeed support this, which means my jsfiddle had a typo or something!编辑——感谢@Tim Down 注意到 WebKit(Chrome 和 Safari)确实支持这一点,这意味着我的 jsfiddle 有错字之类的!

NOTE: This is an experimental technology Check the Browser compatibility table carefully before using this in production.注意:这是一项实验性技术,在生产中使用之前请仔细检查浏览器兼容性表。

Clear Selection:清空选项:

// get a Selection object representing the range of text selected by the user or the current position of the caret.
var selection = window.getSelection();
selection.removeAllRanges();

Set Selection By Node:按节点设置选择:

var selection = window.getSelection();
var range = document.createRange();
    
range.selectNode(nodeToSelect);

selection.addRange(range);

Set Selection By Indexes:按索引设置选择:

var selection = window.getSelection();
var range = document.createRange();
    
range.setStart(nodeToSelect, this.startIndex);
range.setEnd(nodeToSelect, this.endIndex);
    
selection.addRange(range);

Get Current Selection获取当前选择

var range = window.getSelection().getRangeAt(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM