Javascript jQuery fadeToggle()错误

Javascript jQuery fadeToggle()错误,javascript,jquery,Javascript,Jquery,因此,我创建了一个登录页面,然后是主主页。我不希望人们在登录后被重定向到主页,所以我使用了 $("#login").click(function(){ $.post("homepage.php",function(data){ var title = data.slice(data.indexOf("<title"),data.indexOf("</title>")); $("html").html(data); wind

因此,我创建了一个登录页面,然后是主主页。我不希望人们在登录后被重定向到主页,所以我使用了

$("#login").click(function(){
    $.post("homepage.php",function(data){
        var title = data.slice(data.indexOf("<title"),data.indexOf("</title>"));
        $("html").html(data);
        window.history.pushState(data,title,"/homepage.php");
    });
});
它不工作,它抛出一个异常

VM8714:3 Uncaught TypeError: Cannot read property 'style' of undefined
但是我检查了一下,点击事件触发了,我做了

 $("#something").click(function(){
    var display = document.getElementById("something-else").style.display;
    if(display == "none") {
        document.getElementById("something-else").style.display = "block";
    }
    else {
        document.getElementById("something-else").style.display = "none";
    }
 });

这起作用了。有什么想法吗?

我试着根据您提供的信息再现场景,效果很好。 login.php:

<!DOCTYPE html>
<html>
<head>
<title>login.php</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#login").click(function(){
        $.post("homepage.php",function(data){
            var title = data.slice(data.indexOf("<title"),data.indexOf("</title>"));
            $("html").html(data);
            window.history.pushState(data,title,"/homepage.php");
        });
    });
});
</script>
</head>
<body>

<button type="button" id="login">Login</button>
</body>
</html>
homepage.php:

<!DOCTYPE html>
<html>
<head>
<title>homepage.php</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#something").click(function(){
        $("#something-else").fadeToggle();
    });
});
</script>
</head>
<body>
<p id="something-else">something-else</p>
<button type="button" id="something">something</button>
</body>
</html>

结论是问题的根源在其他地方。

jQuery不会产生该错误,不管元素是否存在。一定还有别的事。示例:元素存在的地方:它们不存在的地方:以及只有一些东西存在的地方:您必须提供最低限度的示例来复制您的问题本身。无论如何,为什么要使用$html.htmldata;?听起来是个奇怪的想法。我在stackoverflow上看到了。这就是在不重定向的情况下更改url,在不重新加载的情况下更改页面内容的方法好吧,也许其他东西没有加载?
<!DOCTYPE html>
<html>
<head>
<title>homepage.php</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#something").click(function(){
        $("#something-else").fadeToggle();
    });
});
</script>
</head>
<body>
<p id="something-else">something-else</p>
<button type="button" id="something">something</button>
</body>
</html>