Javascript jQuery:根据URL中的#(数字符号)加载某些div

Javascript jQuery:根据URL中的#(数字符号)加载某些div,javascript,jquery,Javascript,Jquery,例如,本网站: 因此它加载注册页面是因为“#register”。 我不想滚动或加载任何div 这是我单击链接时使用的代码。但我希望当有人直接访问mypage.com/#aboutus时也能加载它 var site=location.hash; 如果(站点=“#关于我们”) { $('#content').load('about.php'); } $(文档).ready(函数(){ $('.aboutus')。单击(函数(){ $('#content').load('about.php');

例如,本网站:

因此它加载注册页面是因为“#register”。 我不想滚动或加载任何div

这是我单击链接时使用的代码。但我希望当有人直接访问mypage.com/#aboutus时也能加载它


var site=location.hash;
如果(站点=“#关于我们”)
{
$('#content').load('about.php');
}
$(文档).ready(函数(){
$('.aboutus')。单击(函数(){
$('#content').load('about.php');
});
})
签入javascript:

if (window.location.hash == "value1") //for #value1 fragment in the url.
{
    //load #div1 here
}
else
{
    //load #anotherDiv here
}

并在jQuery(Ajax,一个对话框,…)中加载div。< /p> < p>只使用<代码>位置。哈希< /代码>属性:

var mysite = location.hash;
if(mysite == "#register")
{
  $("#mydiv").html("Your registration field");
}

来源:

您几乎已经拥有了它-您只需将初始检查放在document ready处理程序中,以便在运行时,
#content
存在

<script>
    $(document).ready(function(){
        var site = location.hash;
        if (site == "#about-us") {
            $('#content').load('about.php');
        }
        $('.aboutus').click(function(){
            $('#content').load('about.php');
        });
    });
</script>

$(文档).ready(函数(){
var site=location.hash;
如果(站点=“#关于我们”){
$('#content').load('about.php');
}
$('.aboutus')。单击(函数(){
$('#content').load('about.php');
});
});

请看我编辑的问题。谢谢-这使问题变得更简单:)
<script>
    $(document).ready(function(){
        var site = location.hash;
        if (site == "#about-us") {
            $('#content').load('about.php');
        }
        $('.aboutus').click(function(){
            $('#content').load('about.php');
        });
    });
</script>