qq_39515511
何止。
2019-11-17 22:13
采纳率: 0%
浏览 131

求助!!怎么把这段JS代码转换成JQuery

<!DOCTYPE html>
<html lang="en">
<head>
<style>
.copy-link{width:90px;height:30px;background:#FF0000;margin:15px auto 0;font-size: 15px;color:#FFFFFF;line-height: 30px;text-align: center;cursor:pointer;border-radius:25px; display:inline-block;}
.link-box{display:inline-block;}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>

<div class="link-box" id="link-box" >https://www.suansuanru.cc/s/iPgQp</div>
<div onselectstart="return false" class="copy-link" >复制链接</div>

</body>
<script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.js"></script>
<script src="clipboard.js"></script>
<script>
$('.copy-link').click(function(){
copyTxt('link-box');
});
// 复制功能
function copyTxt(con) {
const range = document.createRange();
range.selectNode(document.getElementById(con));
const selection = window.getSelection();
if(selection.rangeCount > 0) selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
alert("复制成功!");
}
</script>
</html>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • weixin_43842660
    纳路透 2019-11-20 10:22

    function copyTxt(con) {
    const range = document.createRange();
    range.selectNode($('#'+con)[0];
    const selection = window.getSelection();
    if(selection.rangeCount > 0) selection.removeAllRanges();
    selection.addRange(range);
    document.execCommand('copy');
    alert("复制成功!");
    }

    点赞 评论

相关推荐