基于HTML&JS实现带用户姓名的问候语复制功能问题求助
解决复制粘贴工具的姓名同步问题
你当前的代码里,生成问候语后只更新了#result元素的文本,但没有同步到复制区域的输入框里,同时HTML里的输入框类型和初始值写法也有错误,导致无法正常同步。以下是修复后的完整方案:
关键修改点
- 修正HTML中复制输入框的类型:将
type="document"改为type="text"(输入框的标准类型) - 在生成问候语的函数中,添加同步复制输入框值的代码
- 优化JS中冗余的参数和日志输出逻辑
修改后的完整代码
HTML
<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="text" class="text" placeholder="Hi there, [name]"> <button><i class="fa fa-clone"></i></button> </div>
CSS(无需修改)
@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; }
JavaScript
$(document).ready(function() { var greeting = function() { var name = $('#enterName').val(); var greetingText = 'Hi there, ' + name; console.log(greetingText); $('#result').text(greetingText); // 同步问候语到复制输入框 $('.copy-text input.text').val(greetingText); }; $('#submit_this').click(function() { greeting(); }); }) 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); });
功能说明
- 用户输入姓名后点击提交,生成的问候语会同时显示在
#result区域和复制输入框中 - 点击复制按钮,即可一键复制带有姓名的问候语,同时会显示"Copied"提示
- HTML中用占位符替代了原来错误的初始值,提升了用户体验
内容的提问来源于stack exchange,提问作者Darragh
相关产品推荐
相关产品推荐

