那麼,當我在網上搜索一些基本的代碼來檢查 - 我發現了這一個。一個簡單的代碼,應該複製選定的文本。由於我是JS中的一名完全新手,我檢查了我不明白的方法的含義,並重新編寫了代碼,因爲我做了一些調整。創建一個「複製按鈕」
仍然代碼不工作,如果有人可以解釋 - 這部分「」copyit(this.form.select1)「」 - 即使我有點理解「這個」 - 我無法理解什麼是doind這裏
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>