为什么以下代码没有转到新页面?
$(document).ready(function(){
$(document.body).click(function(){
window.location.href="who.html" ;
});
});
我没有看到任何直接错误,我尝试过使用 $(body) $('body') 和 $('#body') 并且每当我点击页面的正文时,我都不会进入下一个页。
为什么以下代码没有转到新页面?
$(document).ready(function(){
$(document.body).click(function(){
window.location.href="who.html" ;
});
});
我没有看到任何直接错误,我尝试过使用 $(body) $('body') 和 $('#body') 并且每当我点击页面的正文时,我都不会进入下一个页。
检查这个JSFiddle,你可以看到如果你点击“测试”它工作得很好,否则它不起作用。为什么?因为body
里面有一些内容,而不是整个页面(所以如果正文为空,click
则永远不会触发事件)。如果您希望在单击时重定向整个页面,则必须绑定$(document)
.
$(document).click(function() {
window.location.href = "who.html";
});
你可以简单地这样做:
$(document).ready(function(){
$(document).click(function(){
console.log('Document is clicked!');
window.location.href="who.html" ;
});
});