与window.getSelection()相反

时间:2018-09-19 13:36:54

标签: javascript

我正在使用 window.getSelection()。addRange(range)选择要复制的文本:

  var targetelement = document.getElementById("someid"), 
  range = document.createRange();
  range.selectNode(targetelement);
  window.getSelection().addRange(range);
  document.execCommand('copy')

它可以工作,但是浏览器(firefox)在代码完成后选择标签内的文本。我该如何选择呢? window.getSelection 函数或方法是否与之相反?

3 个答案:

答案 0 :(得分:1)

<div id="someid">
This is a test man
</div>

<div id="empty">
</div>


<script>
    var targetelement = document.getElementById("someid");
    var  range = document.createRange();
  range.selectNode(targetelement);
  window.getSelection().addRange(range);
  document.execCommand('copy');  

   window.getSelection().removeAllRanges();
</script>

https://jsfiddle.net/s1teLukn/5/

答案 1 :(得分:1)

复制后,触发focusblur到此输入字段以取消选择文本。

答案 2 :(得分:1)

此旧代码可在所有浏览器中使用:

with a (status, date, amount, next_status) as (
  select status, date, amount,
      lead(status) over(order by date) as next_status
    from my_table
  )
select * from a  
  where status = 'c' and next_status <> 'n'