我正在制作一个简单的数据 URL 生成器,我想知道为什么我的清除按钮的代码不起作用,而其他具有完全相同代码的部分却可以正常工作。
function go() {
var string = document.getElementById("input").value;
var encodedString = btoa(string);
document.getElementById("output").value = "data:text/html;base64," + encodedString;
}
function copy() {
var copyText = document.getElementById("output");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
}
function clear() {
document.getElementById("output").value = "";
document.getElementById("input").value = "";
}
var input = document.getElementById("input");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("go").click();
}
});
button {
font-family: 'Baloo 2', cursive;
background-color: lightgrey;
}
input {
font-family: 'Baloo 2', cursive;
}
<link href="https://fonts.googleapis.com/css?family=Baloo+2&display=swap" rel="stylesheet">
<input id="input" type="text" size="160" placeholder="HTML here" value="">
<button onclick="clear()">🞪</button>
<button id="go" onclick="go()">Convert</button>
<br><br>
<input type="text" placeholder="Output here" id="output" size="160">
<button onclick="copy()">Copy text</button>
在底部附近,名为“clear”的功能不起作用。当我点击调用该函数的按钮时,没有任何反应。
但是,我在函数“go”中使用了完全相同的代码,该代码正在运行。
最佳答案
当您使用内联处理程序时,处理程序为 wrapped inside two with
statements :一个用于处理程序所在的元素,另一个用于文档。您的代码正在执行以下操作:
with (document) {
with (button) {
clear();
}
}
但是文档中存在clear
:
console.log(document.clear);
因此,当您引用名为 clear
的标识符时,它永远不会超出 with
范围,因此您调用的函数clear
永远不会运行。
使用不同的变量名称,例如clearInputAndOutput
:
function go() {
var string = document.getElementById("input").value;
var encodedString = btoa(string);
document.getElementById("output").value = "data:text/html;base64," + encodedString;
}
function copy() {
var copyText = document.getElementById("output");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
}
function clearInputAndOutput() {
document.getElementById("output").value = "";
document.getElementById("input").value = "";
}
var input = document.getElementById("input");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("go").click();
}
});
button {
font-family: 'Baloo 2', cursive;
background-color: lightgrey;
}
input {
font-family: 'Baloo 2', cursive;
}
<link href="https://fonts.googleapis.com/css?family=Baloo+2&display=swap" rel="stylesheet">
<input id="input" type="text" size="160" placeholder="HTML here" value="">
<button onclick="clearInputAndOutput()">🞪</button>
<button id="go" onclick="go()">Convert</button>
<br><br>
<input type="text" placeholder="Output here" id="output" size="160">
<button onclick="copy()">Copy text</button>
但是使用 Javascript 正确附加事件处理程序会更好。内联处理程序有太多范围和转义问题,最好避免。使用 addEventListener
代替,您不必担心名称冲突或全局变量问题:
const [clearBtn, goBtn, copyBtn] = document.querySelectorAll('button');
clearBtn.addEventListener('click', () => {
document.getElementById("output").value = "";
document.getElementById("input").value = "";
});
goBtn.addEventListener('click', () => {
var string = document.getElementById("input").value;
var encodedString = btoa(string);
document.getElementById("output").value = "data:text/html;base64," + encodedString;
});
copyBtn.addEventListener('click', () => {
var copyText = document.getElementById("output");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
});
var input = document.getElementById("input");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("go").click();
}
});
button {
font-family: 'Baloo 2', cursive;
background-color: lightgrey;
}
input {
font-family: 'Baloo 2', cursive;
}
<link href="https://fonts.googleapis.com/css?family=Baloo+2&display=swap" rel="stylesheet">
<input id="input" type="text" size="160" placeholder="HTML here" value="">
<button>🞪</button>
<button id="go">Convert</button>
<br><br>
<input type="text" placeholder="Output here" id="output" size="160">
<button>Copy text</button>
关于javascript - 与JS不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60795137/