<input type="text" id="txtNum"> <button onClick="copyTxt()">点击复制代码</button>
//复制事件; function copyTxt() { var txt = document.getElementById("txtNum"); txt.select(); // 选择对象 document.execCommand("Copy"); //执行浏览器复制命令 alert("已复制好,可贴粘!"); window.location.href = "http://www.baidu.com"; } //登录提示复制; function tips() { //提示复制; alert("请复制数字"); } tips(); //随机数 function getNum() { var num = parseInt(Math.random() * 10 + 9); console.log(num); document.getElementById("txtNum").value = num; } getNum();
Done!