2

为什么以下代码没有转到新页面?

$(document).ready(function(){
    $(document.body).click(function(){
        window.location.href="who.html" ;
    });
});

我没有看到任何直接错误,我尝试过使用 $(body) $('body') 和 $('#body') 并且每当我点击页面的正文时,我都不会进入下一个页。

4

2 回答 2

12

检查这个JSFiddle,你可以看到如果你点击“测试”它工作得很好,否则它不起作用。为什么?因为body里面有一些内容,而不是整个页面(所以如果正文为空,click则永远不会触发事件)。如果您希望在单击时重定向整个页面,则必须绑定$(document).

$(document).click(function() {
    window.location.href = "who.html";
});
于 2013-05-26T09:43:35.413 回答
0

你可以简单地这样做:

$(document).ready(function(){
    $(document).click(function(){
        console.log('Document is clicked!');
        window.location.href="who.html" ;
    });
});
于 2013-05-26T09:44:41.050 回答