程序员开发实例大全宝库

网站首页 > 编程文章 正文

js 实现代码复制(js复制文本代码)

zazugpt 2024-08-17 01:57:47 编程文章 26 ℃ 0 评论
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<style type="text/css">
	   .wrapper {position: relative;}
	   #input {position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;}
	</style>
	<script src="/assets/libs/jquery/jquery-3.2.1.min.js"></script>
</head>
<body>
    <div class="wrapper">
	   <button class="copy">copy</button>
       <textarea>我是需要复制的文本内容</textarea>
    </div>
</body>
<script type="text/javascript">
	$(function(){
		$(document).on('click', ".copy", function() {
				$(this).next("textarea").select();
				document.execCommand("Copy");
				alert("复制成功!");
		});
	});
</script>
</html>
var items = [];
$.each(data, function(key, value){
  //console.log(key);
  if (key.indexOf("Code")>=0 && value){
    var highCode = hljs.highlightAuto(value).value;
    items.push({title: key.replace("Code","") , content: "<pre class='layui-code' style='margin-top:0px'>" +
                "<button class='copy layui-btn layui-btn-sm' style='float:right;z-index: 9999'>复制</button>" +
                "<textarea style='opacity: 0;z-index: -10;float:right'>"+ value +"</textarea>" +
                "<code>" + highCode + "</code>" +
                "</pre>"})
  }
});
top.layer.tab({
  area: ['80%', '80%'],
  shadeClose: true,
  maxmin: true,
  success: function(layero, index){
    parent.loadCss("/assets/libs/highlight/default.min.css");
    layero.find('pre').on('click', ".copy", function() {
      $(this).next("textarea").select();
      alert($(this).next("textarea").val());  
      top.window.document.execCommand("Copy");
      alert("复制成功!");
    });
  },
  tab: items
});   

注意:top.layer.tab 要使用top.window.document.execCommand("Copy");

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表