jQuery fadeToggle() error

jQuery fadeToggle() error

本文关键字:error fadeToggle jQuery      更新时间:2023-09-26

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

$("#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");
    });
});

这很好用,但错误在于主页.php我有

$("#something").click(function(){
    $("#something-else").fadeToggle();
});

它不起作用,它会抛出异常

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";
    }
 });

这奏效了。有什么想法吗?

我试图根据您提供的信息重现场景,效果很好。登录.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>

首页.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>

判决是问题的根源在别的地方。