gpt4 book ai didi

javascript - 如果输入值为字符串,则更改窗口位置

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

我正在尝试制作一个迷你 Google。您在 Facebook 或 Instagram 中输入的地方,当您单击“搜索”按钮时,您将被发送到该页面。到目前为止,我已经得到了这个,但它只会转到 Facebook。

var btn = document.getElementById("btn");
var search = document.getElementById("search")

btn.onclick = function (){

if (search.value == "Facebook" || "facebook"){
window.location = "https://www.facebook.com/";

} else if( search.value == "Instagram" || "instagram"){
window.location = "https://www.instagram.com/";
}
}
<input id="search" type="text"/>
<button id="btn" type="button">Search</button>

最佳答案

问题出在你的if上,if (search.value == "Facebook"|| "facebook")就像在说

if search.value == "Facebook" is true, or "facebook" is true, do

这是行不通的,因为 "facebook" 将始终为真,因此永远不会输入 Instagram 的第二个 if

相反,你想要的是

if search.value == "Facebook" is true, or if search.value == "facebook" is true, do

var btn = document.getElementById("btn");
var search = document.getElementById("search")

btn.onclick = function (){

if (search.value == "Facebook" || search.value == "facebook"){
window.location = "https://www.facebook.com/";

} else if( search.value == "Instagram" || search.value == "instagram"){
window.location = "https://www.instagram.com/";
}
}
<input id="search" type="text"/>
<button id="btn" type="button">Search</button>

关于javascript - 如果输入值为字符串,则更改窗口位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52524297/

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