我是编码的新手。我想在我当前的代码中添加更多的变量。现在,如果你输入“dog”,它就会打开YouTube。例如,你如何添加另一个变量,这样,如果你输入dog,它仍然会打开youtube,但如果你输入“cat”,它会打开Netflix?你是怎么做到的?感谢所有帮助你的人。请记住,我仍然是一个新的编码,所以请不要批评。
const myFunction = () => {
if (document.getElementById('textInput').value === 'dog'){
window.open(
"https://youtube.com", "_blank");
}
}
<input type="text" id="textInput" oninput="myFunction()">
您可以使用switch case来处理您的解决方案
switch(document.getElementById('textInput').value) {
case 'dog':
window.open(
"https://youtube.com", "_blank");
break;
case 'cat':
window.open(
"https://google.com", "_blank");
break;
default:
// When you write a word that you did not consider (in this example, except for
dogs and cats, enter this block)
}