分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

js实现copy

发布时间:2023-09-06 01:07责任编辑:彭小芳关键词:js

  在js中实现copy。看似很简单的功能。实际上却有点复杂。因为只有能select的元素或者contentEditable的元素才支持document.execCommand(‘copy‘),所以经过百度之后发现了

range这么个属性

 function copy(copyEle) { ???????if (copyEle.nodeName.toLowerCase() === "input") { ???????????copyEle.select(); ???????????document.execCommand(‘copy‘); ???????} else { ???????????var range = document.createRange(); ???????????var selection = window.getSelection(); ???????????range.selectNode(document.getElementById(‘href‘)); ???????????if (selection.rangeCount > 0) selection.removeAllRanges(); ???????????selection.addRange(range); ???????????document.execCommand(‘copy‘); ???????????selection.removeAllRanges(); ???????} ???} ???document.querySelector("#copy").addEventListener("click", function() { ???????var copyElement = document.querySelector("#href"); ???????copy(copyElement); ???}, false);

目前不支持安卓

js实现copy

原文地址:http://www.cnblogs.com/yuweia/p/7605529.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved