<input type="text" id="url" name="url" placeholder="请输入网址">
<button onclick="goToUrl()">网址导航</button>
<script>
function goToUrl() {
var url = document.getElementById("url").value;
// 检查是否以http或https开头,如果不是,则添加https://
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url;
}
window.location.href = url;
}
</script>
修改以下代码,让用户只有输入abc或者def时候才能跳转,并且输入abc跳转到a.com 输入def跳转到u.com 输入其他字符均不跳转
<input type="text" id="url" name="url" placeholder="请输入指令">
<button onclick="goToUrl()">导航</button>
<script>
function goToUrl() {
var input = document.getElementById("url").value;
var url;
if (input === "abc") {
url = "https://a.com";
} else if (input === "def") {
url = "https://u.com";
} else {
// 如果输入的不是abc或def,则不跳转
alert("请输入abc或def进行导航");
return;
}
window.location.href = url;
}
</script>
网站跳转