gpt4 book ai didi

javascript - 创建一个 "Copy button"

转载 作者:行者123 更新时间:2023-11-30 15:31:53 25 4
gpt4 key购买 nike

好吧,当我在 Internet 上搜索一些要检查的基本代码时 - 我找到了这个。应该复制所选文本的简单代码。由于我是 JS 的新手,我检查了我不理解的方法的含义 - 并重写了代码,同时做了一些调整。

而且代码仍然无法正常工作,如果有人可以解释 - 这部分“”copyit(this.form.select1)” - 尽管我有点理解“this” - 我无法理解正在做什么这里

function copyit(theField) {
var selectedText = document.getSelection();
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.focus();
theField.value = newRange.text;
} else {
alert('select a text in the page and then press this button');
}
}
</script>



<form name="it">
<div align="center">
<input onclick="copyit(this.form.select1)" type="button" value="Press to copy the highlighted text" name="btnCopy">
<p>
<textarea name="select1" rows="4" cols="45"></textarea>
</div>
</form>

这是原始代码 - 但它也不起作用

<SCRIPT LANGUAGE="JavaScript">
function copyit(theField) {
var selectedText = document.selection;
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.focus();
theField.value = newRange.text;
} else {
alert('select a text in the page and then press this button');
}
}
</script>


And in the body of your web page, add the following where you want the text to appear:
<form name="it">
<div align="center">
<input onclick="copyit(this.form.select1)" type="button" value="Press to copy the highlighted text" name="btnCopy">
<p>
<textarea name="select1" rows="4" cols="45"></textarea>
</div>
</form>

最佳答案

onclick="copyit(this.form.select1)"

执行 copyit() 函数并传递一个变量,该变量稍后命名为 theField。传递的变量是 this.form.select1,它是一个 ID 为 select1 的文本区域,它位于与您单击的输入相同的表单中,因此 this.form.

至于为什么您的代码不起作用 - 您应该在此处包含调整前的原始代码。您可能删除/更改了不应该拥有的内容。

关于javascript - 创建一个 "Copy button",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42122073/

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