Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用URL参数触发Javascript_Javascript_Jquery - Fatal编程技术网

使用URL参数触发Javascript

使用URL参数触发Javascript,javascript,jquery,Javascript,Jquery,我正在使用此片段在单击特定链接时将内容淡入div <script language="JavaScript"> $(document).ready(function () { $('#section1, #section2, #section3').addClass('js'); $('#content-container a').click(function (e) { e.preventDefault();

我正在使用此片段在单击特定链接时将内容淡入div

<script language="JavaScript">
    $(document).ready(function () {
        $('#section1, #section2, #section3').addClass('js');
        $('#content-container a').click(function (e) {
            e.preventDefault();
            var rel = $(this).attr('rel');
            $('#' + rel).fadeIn().siblings('div').fadeOut();
        });
        var link = document.URL.split('#')[1];
        if (link) {
            var el = $('#' + link);
            if (el) el.click();
        }
    });
</script>

$(文档).ready(函数(){
$('section1,'section2,'section3')。addClass('js');
$(“#内容容器a”)。单击(函数(e){
e、 预防默认值();
var rel=$(this.attr('rel');
$('#'+rel.fadeIn().sides('div').fadeOut();
});
var link=document.URL.split(“#”)[1];
如果(链接){
var el=$(“#”+链接);
如果(el)el.click();
}
});
是否有方法使用URL而不是单击来加载指定的内容?所以我可以链接到www.mydomain.com/mypage.php#section2或类似的网站

更新 下面是一段简化代码

允许您指定页面的以下部分:

$( "#container" ).load( "/mypage.php #section2" );

您可以这样做:

if(window.location.hash && window.location.hash=="#section2") {
    // Do stuff that are meant to happen when this hash is present.
}
试试这个

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>mouseover demo</title>
    <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
    <style>
        .invisible
        {
            display: none;
        }
        .lorem
        {
            height: 300px;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            getContent();
        });
        $(window).bind('hashchange', function (e) {
            getContent();
        });

        function getContent() {
            var url = window.location.toString();
            var hash = url.substring(url.indexOf('#'));
            $('html, body').animate({
                scrollTop: $(hash).offset().top
            }, 2000);
            $(hash).fadeIn();
            $(hash).siblings("div").fadeOut();
        }
    </script>
</head>
<body>
    <div class="lorem">
        <div id="section1" class="invisible">
            Content 1
            <p style="text-align: justify;">
                Content 1 content</p>
        </div>
        <div id="section2" class="invisible">
            Content 2
            <p style="text-align: justify;">
                Content 2 content</p>
        </div>
        <div id="section3" class="invisible">
            Content 3
            <p style="text-align: justify;">
                Content 3 content</p>
        </div>
        <div id="section4" class="invisible">
            Content 4
            <p style="text-align: justify;">
                Content 4 content
            </p>
        </div>
    </div>
    <div id="links">
        <a href="#section1" rel="section1" id="section1">Section 1</a>
        <br />
        <a href="#section2" rel="section2" id="section2">Section 2</a>
        <br />
        <a href="#section3" rel="section3" id="section3">Section 3</a>
        <br />
        <a href="#section4" rel="section4" id="section3">Section 4</a>
    </div>
</body>
</html>

鼠标悬停演示
看不见的
{
显示:无;
}
洛雷姆先生
{
高度:300px;
}
$(文档).ready(函数(){
getContent();
});
$(窗口).bind('hashchange',函数(e){
getContent();
});
函数getContent(){
var url=window.location.toString();
var hash=url.substring(url.indexOf(“#”);
$('html,body')。设置动画({
scrollTop:$(散列).offset().top
}, 2000);
$(hash.fadeIn();
$(散列).sides(“div”).fadeOut();
}
内容1

内容1内容

内容2

内容2内容

内容3

内容3内容

内容4

内容4内容




这就是你要找的吗。加载时,页面将查找#部分以显示内容。单击锚点时,将更改哈希链接,并将哈希更改函数加载到所需内容中


如果您有任何疑问,请告诉我。

链接的值是多少??您是说当人们打开带有“已附加”的URL时?通过执行
el.click()
,您似乎已经在这样做了。也就是说,假装用户点击。你在找这个吗?你有没有尝试过
window.location.href=link
?我不知道如何将它整合到我已有的内容中。我已经用JSFIDLE更新了原始帖子,以更好地验证您的需求。。当url作为“url”#section1调用时,您希望div“section1”淡入。您已经做了一些修改。。希望这对汉克斯有帮助,那很好用。看起来比我以前做的要简单得多