gpt4 book ai didi

Javascript - 在一页上复制多个文本区域的文本按钮

转载 作者:行者123 更新时间:2023-11-30 21:18:00 25 4
gpt4 key购买 nike

我已经搜索了这个站点的类似问题,但我仍然一头雾水。

基本上,我正在为即将离职的同事接手一个项目。他的 Intranet 页面计划应该有多个文本区域,每个文本区域都有自己的预定义文本和自己的“复制文本”按钮。

点击后,它会复制到用户的剪贴板。我拆开了代码,无论出于何种原因,当我添加新的文本区域和按钮时,它都不会复制。第一个会。

我错过了什么或做错了什么?

<html>
<head>
<script>
window.onload = function () {
var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

copyTextareaBtn.addEventListener('click', function (event) {
var copyTextarea = document.querySelector('.js-copytextarea');
copyTextarea.select();

try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Whoops, unable to copy');
}
});
}
</script>
</head>
<body>
<p>Test #1 </p>

<div>
<textarea class="js-copytextarea" readonly="readonly" style="width:20%;"
rows="5">Hello. This is textarea test bed #1</textarea>
<button class="js-textareacopybtn">Copy Text (works)</button>

<p>Test #2:</p>

<textarea class="js-copytextarea" readonly="readonly" style="width:20%;"
rows="5">Hi! Welcome to textarea test bed #2 </textarea>
<button class="js-textareacopybtn">Copy Text (broken)</button>
</div>
</body>
</html>

最佳答案

它不起作用,因为您只将第一个按钮连接到点击事件,因为您只获得对第一个按钮的引用:

var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

将其更改为:

var copyTextareaBtn = document.querySelectorAll('.js-textareacopybtn');

.querySelector() 只返回对它找到的与选择器匹配的第一个元素的引用。 <强> querySelectorAll() 返回一个节点列表,其中包含与选择器匹配的所有元素。

接下来,您需要将点击事件附加到该节点列表中的每个元素,如果您将这些节点列表转换为数组, .forEach() 方法使循环变得非常容易。

查看下面更新的代码:

window.onload = function () {
// Get all the elements that match the selector as arrays
var copyTextareaBtn = Array.prototype.slice.call(document.querySelectorAll('.js-textareacopybtn'));
var copyTextarea = Array.prototype.slice.call(document.querySelectorAll('.js-copytextarea'));

// Loop through the button array and set up event handlers for each element
copyTextareaBtn.forEach(function(btn, idx){

btn.addEventListener("click", function(){

// Get the textarea who's index matches the index of the button
copyTextarea[idx].select();

try {
var msg = document.execCommand('copy') ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Whoops, unable to copy');
}

});

});
}
<div>
<p>Test #1
<textarea class="js-copytextarea" readonly="readonly" style="width:20%;"
rows="5">Hello. This is textarea test bed #1</textarea>
<button class="js-textareacopybtn">Copy Text (works)</button>
</p>

<p>Test #2:
<textarea class="js-copytextarea" readonly="readonly" style="width:20%;"
rows="5">Hi! Welcome to textarea test bed #2 </textarea>
<button class="js-textareacopybtn">Copy Text (broken)</button>
</p>
</div>

关于Javascript - 在一页上复制多个文本区域的文本按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45472971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com