前端JS复制特定区域的文本(兼容safari)
2024-09-29 05:33:19
html5的webAPI接口可以很轻松的使用短短的几行代码就实现点击按钮复制区域文本的功能,不需要依赖flash。
代码如下:
/* 创建range对象 */
const range = document.createRange();
range.selectNode(element); // 设定range包含的节点对象 /* 窗口的selection对象,表示用户选择的文本 */
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges(); // 将已经包含的已选择的对象清除掉
selection.addRange(range); // 将要复制的区域的range对象添加到selection对象中 document.execCommand('copy'); // 执行copy命令,copy用户选择的文本
//兼容Pc端的safari浏览器
let node = document.getElementById('copy');//input框
node.setAttribute('value', 'hello world');
let issafariBrowser = /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent);
if(issafariBrowser){
//safari浏览器单独处理
node.setSelectionRange(0, 9999);
}
else{
//其他浏览器
const range = document.createRange();
range.selectNode(node);
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
} document.execCommand('copy');
还有一种兼容safari和chrome浏览器的通用写法不需要判断,这种写法在demo中可以成功。
demo如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.btn{
cursor: pointer;
width: 200px;
height: 100px;
background: red;
display: inline-block;
}
</style>
<!-- <link type="text/css" rel="styleSheet" href="1.css"> -->
</head>
<body style="background: blue">
<div class="div1"> </div>
<div id="cardList">
<div class="btn" id='btn'>点击我,复制我</div>
<input id='copy'/>
</div> </body> <script>
var btn = document.querySelector('#btn');
btn.addEventListener('click',function(){ let input = document.getElementById('copy'); input.setAttribute('readonly', 'readonly');
input.setAttribute('value', 'hello world'); const range = document.createRange(); range.selectNode(input);
const selection = window.getSelection();
console.log(selection)
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
})
</script> </html>
但是在react项目中,在safari浏览器中
window.getSelection();对象的anchorNode值一直为null,
所以在safari中不成功,
所以最终采用了判断是否为safari浏览器来进行不同操作的方法。
API参考:
最新文章
- 使用abp模板创建解决方案
- .NET 学习书籍推荐
- 推荐cms
- poj1511 最短路
- Linux 继续进阶
- 面试时,问哪些问题能试出一个Android应用开发者真正的水平?
- Java中字符串内存位置浅析
- Web APIs 基于令牌TOKEN验证的实现
- 前台javascript排序
- MySQL注入与防御(排版清晰内容有条理)
- JavaScript图片翻转
- ASP.NET前台table通过Ajax获取绑定后台查询的json数据
- Postgres中postmaster代码解析(中)
- Spring中的@conditional注解
- 如何解决angular不自动生成spec.ts文件
- F2833x 调用DSP函数库实现复数的FFT的方法
- Java类成员变量的默认值
- 1.maven中pom.xml文件中<;exclusions>;标签认不到问题
- Drip is a launcher for the Java Virtual Machine that provides much faster startup times than the java command. The drip script is intended to be a drop-in replacement for the java command, only faster
- 算法笔记_206:第五届蓝桥杯软件类决赛真题(Java语言A组)
热门文章
- MySQL------代码1024,can&#39;t get hostname for your address解决方法
- xc_domain_save.c
- asp.net页面生命周期总结
- JSP内置对象——response
- Python: 使用pip升级所有包
- Codeforces Round #513-ABCD
- Orchard 与 ABP架构比较 (aspnetboilerplate)
- JQueryiframe页面操作父页面中的元素与方法(实例讲解)
- How TCP clients and servers communicate using the TCP sockets interface
- Flask用Flask-SQLAlchemy连接MySQL