我正在尝试创建一个复制粘贴表,它将首先要求用户输入(他们的名字)。
提交名称后,理想情况下它将在复制到剪贴板按钮中使用,如下面的 HTML/JS 所示。
最终我希望输入名称的整个问候语可以轻松复制和粘贴
例如
你好,#name .
目前用户输入正在工作,但我正在努力将其复制到带有用户名的剪贴板文本区域。
https://codepen.io/dmcp123/pen/gOBYQRb
$(document).ready(function() {
var greeting = function(name) {
var name = $('#enterName').val();
name = 'Hi there,' + ' ' + name;
console.log('Hi there,' + '' + name);
$('#result').text(name);
};
$('#submit_this').click(function() {
greeting();
//prompt('Hi there,' + '' + name);
});
})
let copyText = document.querySelector(".copy-text");
copyText.querySelector("button").addEventListener("click", function () {
let input = copyText.querySelector("input.text");
input.select();
document.execCommand("copy");
copyText.classList.add("active");
window.getSelection().removeAllRanges();
setTimeout(function () {
copyText.classList.remove("active");
}, 2500);
});
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
*{
box-sizing: border-box;
}
body {
font-family: "Roboto", sans-serif;
background: #F7EDE2;
}
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.label {
padding: 10px;
font-size: 18px;
color: #111;
}
.copy-text {
position: relative;
padding: 10px;
background: #fff;
border: 1px solid #ddd;
border-radius: 10px;
display: flex;
}
.copy-text input.text {
padding: 10px;
font-size: 18px;
color: #555;
border: none;
outline: none;
}
.copy-text button {
padding: 10px;
background: #5784f5;
color: #fff;
font-size: 18px;
border: none;
outline: none;
border-radius: 10px;
cursor: pointer;
}
.copy-text button:active {
background: #809ce2;
}
.copy-text button:before {
content: "Copied";
position: absolute;
top: -45px;
right: 0px;
background: #5c81dc;
padding: 8px 10px;
border-radius: 20px;
font-size: 15px;
display: none;
}
.copy-text button:after {
content: "";
position: absolute;
top: -20px;
right: 25px;
width: 10px;
height: 10px;
background: #5c81dc;
transform: rotate(45deg);
display: none;
}
.copy-text.active button:before,
.copy-text.active button:after {
display: block;
}
footer {
position: fixed;
height: 50px;
width: 100%;
left: 0;
bottom: 0;
background-color: #5784f5;
color: white;
text-align: center;
}
footer p {
margin: revert;
padding: revert;
}
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<form>
<div>Your name:</div>
<input name="enterName" id="enterName" type="text">
<div style="margin-top:10px;" class="submit">
<input value="submit" id="submit_this" type="button">
</div>
</form>
<br>
<div id="result"></div>
<div class="copy-text">
<input type="document" class="text" value="(Hi there, " + name>
<button><i class="fa fa-clone"></i></button>
</div>