Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何恢复选择?_Javascript - Fatal编程技术网

Javascript 如何恢复选择?

Javascript 如何恢复选择?,javascript,Javascript,我正在使用此代码将一些文本从复制到剪贴板: function copy() { var txtInput = document.getElementById('txtInput'); txtInput.select(); var success; try { success = document.execCommand('copy'); } catch(err) { success = false; } if

我正在使用此代码将一些文本从
复制到剪贴板:

function copy() {
    var txtInput = document.getElementById('txtInput');
    txtInput.select();
    var success;
    try {
        success = document.execCommand('copy');
    } catch(err) {
        success = false;
    }
    if(!success) {
        alert('Copy failed');
    }
}
但是,我不想干扰用户的选择。我怎样才能把它恢复到以前的样子

我只需要支持最新版本的Safari


澄清:我想复制所有文本(就像这个函数那样),但不改变它们现有的选择。

我现在明白了。这应该至少在Chrome 50+中起作用。它将允许您突出显示文本、复制文本、保持突出显示并确保其在剪贴板上

function getSelectionText() {
  var txtInput = document.getElementById('txtInput');

  if (txtInput.selectionStart != txtInput.selectionEnd) { // check the user has selected some text inside field
    //get actual text
    var selectedtext = txtInput.value.substring(txtInput.selectionStart, txtInput.selectionEnd);
    //set original highlight
    txtInput.setSelectionRange(txtInput.selectionStart, txtInput.selectionEnd)
    return selectedtext;
  }
}

function copy() {
  //check our log to be sure
  console.log(getSelectionText());
  try {
    window.document.execCommand('copy');
  } catch (err) {
    alert('Copy failed');
  }
}
试一试:

这里有一个参考:

更新:根据评论

function getSelectionText(cb) {
  var txtInput = document.getElementById('txtInput');

  if (txtInput.selectionStart != txtInput.selectionEnd) { // check the user has selected some text inside field
    //get selected text
    var selectedtext = txtInput.value.substring(txtInput.selectionStart, txtInput.selectionEnd);
    var partial = {
      start: txtInput.selectionStart,
      end: txtInput.selectionEnd
    };

    //get all text
    var allText = txtInput.select();

    cb();

    //set original highlight
    txtInput.setSelectionRange(partial.start, partial.end);
  }
}

function copy() {

    console.log('copying')
  getSelectionText(function() {
    //check our log to be sure
    console.log('callback');
    try {
      window.document.execCommand('copy');
    } catch (err) {
      alert('Copy failed');
    }
  })
}

为什么不抓取文本,存储在变量中,然后将其重置为该变量?恢复整个文本区域,或文本区域的选定部分?此外,我认为safari 9不支持它。1@JordanHendrix什么意思?我不会改变课文的。我正在通过
txtInput.select()
覆盖用户的现有选择,这是我想要避免的。我想你误解了我的要求。我想复制所有文本,但如果它们有选择,则不更改它们的选择。据我所知,如果不先选择文本,就无法复制文本,因此我必须选择所有文本,复制文本,然后重置它们的选择。这看起来正是我想要的。非常感谢。