javascript - 为什么我的“复制到剪贴板”按钮不起作用?
问题描述
Web 编程的新手。我尝试重新创建一些网站,我想实现一个“复制到剪贴板”按钮,但它不起作用,我不知道为什么。我已经通过 Stackoverflow 进行了搜索,但我找不到这个问题的答案。
在此先感谢您的帮助!!
HTML:
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Portfolio Homepage</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="nav-wrapper">
<div class="left-side">
<div class="nav-link-wrapper">
<a href="index.html">Home</a>
</div>
<div class="nav-link-wrapper active-nav-link">
<a href="about.html">About</a>
</div>
</div>
<div class="right-side">
<div class="brand">
ptp
</div>
</div>
</div>
<div class="content-wrapper">
<div class="two-column-wrapper">
<div class="profile-image-wrapper">
<img src="images/profile.jpg" alt="">
</div>
<div class="profile-content-wrapper">
<h1>Managemt and Additional Information</h1>
<p>This is a student's non-profit project made solemnly for practice and community involvement. If you have any further inquiry about this project, please contact placeholdermail. </p>
<p>Thank you very much!</p>
</div>
</div>
</div>
</div>
</body>
</html>
JavaScript:
function copythis() {
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
解决方案
我看到您一直在尝试从W3school运行示例
现在,您的代码有什么问题:您没有定义myInput
inHTML
以便它可以从 HTML DOM 中获取文本。
下面是工作代码。
function myFunction() {
var copyText = document.getElementById("myInput");
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText.value);
}
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Portfolio Homepage</title>
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<div class="nav-wrapper">
<div class="left-side">
<div class="nav-link-wrapper">
<a href="index.html">Home</a>
</div>
<div class="nav-link-wrapper active-nav-link">
<a href="about.html">About</a>
</div>
</div>
<div class="right-side">
<div class="brand">
ptp
</div>
</div>
</div>
<div class="content-wrapper">
<div class="two-column-wrapper">
<div class="profile-image-wrapper">
<img src="images/profile.jpg" alt="">
</div>
<div class="profile-content-wrapper">
<h1>Managemt and Additional Information</h1>
<p>This is a student's non-profit project made solemnly for practice and community involvement. If you have any further inquiry about this project, please contact placeholdermail.
<br>
<input type="text" value="Hello World" id="myInput">
<button onclick="myFunction()">Copy text</button>
</p>
<p>Thank you very much!</p>
</div>
</div>
</div>
</div>
</body>
</html>
推荐阅读
- javascript - DIVI 中的 Javascript:mouseenter 不起作用
- javascript - Workbox 运行时缓存不适用于带有查询参数的 url?
- reactjs - redux 在反应钩子中调度 prevState 值
- python - 无法使用未更改的 url 抓取网站页面 - python
- asp.net - 将会话超时设置为特定时间而不是长度?
- javascript - Three.js Fiber / Drei - 如何加载 fbx 模型?
- python - 对齐 sympy 的乳胶输出?
- amazon-web-services - 如何允许 AWS Textract 访问受保护的 S3 存储桶
- javascript - 警告:
使用了不正确的大小写。对 React 组件使用 PascalCase,对 HTML 元素使用小写 - javascript - 反转触摸设备上的手势 [Autodesk forge 查看器]