var li1 = document.querySelector("nav ul li:nth-child(1)") li1.onclick = function(){ window.location.href = ["index.html"] } var li2 = document.querySelector("nav ul li:nth-child(2)") li2.onclick = function(){ window.location.href = ["2.html"] } var li3 = document.querySelector("nav ul li:nth-child(3)") li3.onclick = function(){ window.location.href = ["3.html"] } var li4 = document.querySelector("nav ul li:nth-child(4)") li4.onclick = function(){ window.location.href = ["4.html"] }有报错
时间: 2024-03-15 17:44:27 浏览: 332
TypeError document.getElementById(...) is null错误原因
根据你提供的代码,我并没有看到明显的报错。但是,我注意到你在 `window.location.href` 中传递的参数是一个数组,而不是一个字符串,这可能会导致跳转不成功。你可以把数组改为字符串,例如:
```
li1.onclick = function(){
window.location.href = "index.html";
}
li2.onclick = function(){
window.location.href = "2.html";
}
li3.onclick = function(){
window.location.href = "3.html";
}
li4.onclick = function(){
window.location.href = "4.html";
}
```
这样就可以正常跳转页面了。
阅读全文