html:
<div class="head"> <p>个人中心</p> </div> </header> <div class="copytext"> <div class="visit"> 邀请ID:<p class="V-P">96045638</p> <input type="text" value="" style="opacity: 0;"> <button>复制</button> </div> </div>
css:
*{ margin: 0; padding: 0; } header{ height: 40px; } .head{ width: 100%; height: 40px; position: fixed; top: 0; background-color: #fff; display: flex; align-items: center; justify-content: center; } .copytext{ width: 100%; height: 300px; background-color: rgb(51,51,51); } .visit{ /* color: rgb(241,211,162); */ color: rgb(248,249,249); display: flex; } .visit button{ color: rgb(241,211,162); border: 2px solid rgb(241,211,162); background-color: rgb(51,51,51); } .visit input{ width: 10px; height: 10px; }
js:
let but = document.querySelector('button');//获取到标签 let text = document.querySelector('.V-P'); let texta = document.querySelector('input'); console.log(texta); but.addEventListener('click', function() {//绑定一个点击函数 texta.value = text.innerHTML; console.log(texta.value); texta.select(); document.execCommand("copy");//依靠execCommand来完成指令 });