Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.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
Javascript Fancybox:更改地址栏中url的链接,并链接到打开Fancybox的页面_Javascript_Jquery_Fancybox - Fatal编程技术网

Javascript Fancybox:更改地址栏中url的链接,并链接到打开Fancybox的页面

Javascript Fancybox:更改地址栏中url的链接,并链接到打开Fancybox的页面,javascript,jquery,fancybox,Javascript,Jquery,Fancybox,基本上,我需要的是获得fancybox链接,以将地址栏中的url更改为如下内容: www.website.com/page/#lightbox 此外,如果我访问www.website.com/page/#lightbox,则与该链接对应的lightbox将自动打开 这方面的一个很好的例子是在这个网站上: 诀窍是基于某个标签创建一个函数来显示所需内容。最简单的方法是使hashtag的值也成为您希望在fancybox中显示的页面上某个元素的ID(尽管不是必需的) 然后,只需对链接HREF使用hash

基本上,我需要的是获得fancybox链接,以将地址栏中的url更改为如下内容: www.website.com/page/#lightbox

此外,如果我访问www.website.com/page/#lightbox,则与该链接对应的lightbox将自动打开

这方面的一个很好的例子是在这个网站上:

诀窍是基于某个标签创建一个函数来显示所需内容。最简单的方法是使hashtag的值也成为您希望在fancybox中显示的页面上某个元素的ID(尽管不是必需的)

然后,只需对链接HREF使用hashtags,不要在这些链接上使用jQuery的
preventDefault()
函数。这样,这些链接将改变url栏,附加您设置为其href值的哈希标记。然后,将显示fancybox的功能附加到这些链接上

最后,用
location.hash
作为参数调用该函数

这只是一个简单的例子。似乎做了你想做的事

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
    <script src="fb/fb.js" type="text/javascript"></script>
    <link rel="stylesheet" href="fb/fb.css" type="text/css" media="screen" charset="utf-8" />
    <script type="text/javascript" charset="utf-8">
        $(function(){
                function showfb(id) {
                    switch(id) {
                    case '#fb1':
                    case '#fb2':
                        // Gotta do this so that fb can measure the content dimensions
                        $(id).show();
                        $.fancybox({
                            href: id,
                            type:'inline',
                            // This is so that the content doesn't just pop back on to the
                            // page when finished. Of course, if you're not using inline content
                            // then this may not apply
                            onClosed: function() {
                                $(id).hide();
                            }
                        });
                        break;
                    }
                }
                showfb(location.hash);
                $('a.fblink').click(function(e){
                        showfb($(this).attr('href'));
                });
        });
    </script>
    <style type="text/css" media="screen">
    /* I'm assuming you want the fancybox content to 
    ONLY be displayed in the fancybox. Once again, if using 
    ajax or some other method of getting fancybox content, this might
    not be necessary */
        .fb { display: none; }
    </style>
</head>
<body>

    <p>
        <a class='fblink' href="#fb1">Show 1</a>
    </p>
    <p>
        <a class='fblink' href="#fb2">Show 2</a>
    </p>

    <div id="fb1" class="fb">This is a test</div>
    <div id="fb2" class="fb">This is another test</div>

</body>
</html>

$(函数(){
函数showfb(id){
开关(id){
案例“fb1”:
案例“#fb2”:
//必须这样做,以便fb可以测量内容维度
$(id.show();
$.fancybox({
href:id,
类型:'inline',
//这样一来,内容就不会突然出现在屏幕上
//当然,如果你没有使用内联内容
//那么这可能不适用
onClosed:function(){
$(id).hide();
}
});
打破
}
}
showfb(location.hash);
$('a.fblink')。单击(函数(e){
showfb($(this.attr('href'));
});
});
/*我想你希望fancybox的内容
只能在fancybox中显示。再一次,如果使用
ajax或其他获取fancybox内容的方法,这可能
没必要*/
.fb{显示:无;}

这是一个测试 这是另一个测试
诀窍是基于一些标签,创建一个函数来显示您想要的内容。最简单的方法是使hashtag的值也成为您希望在fancybox中显示的页面上某个元素的ID(尽管不是必需的)

然后,只需对链接HREF使用hashtags,不要在这些链接上使用jQuery的
preventDefault()
函数。这样,这些链接将改变url栏,附加您设置为其href值的哈希标记。然后,将显示fancybox的功能附加到这些链接上

最后,用
location.hash
作为参数调用该函数

这只是一个简单的例子。似乎做了你想做的事

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
    <script src="fb/fb.js" type="text/javascript"></script>
    <link rel="stylesheet" href="fb/fb.css" type="text/css" media="screen" charset="utf-8" />
    <script type="text/javascript" charset="utf-8">
        $(function(){
                function showfb(id) {
                    switch(id) {
                    case '#fb1':
                    case '#fb2':
                        // Gotta do this so that fb can measure the content dimensions
                        $(id).show();
                        $.fancybox({
                            href: id,
                            type:'inline',
                            // This is so that the content doesn't just pop back on to the
                            // page when finished. Of course, if you're not using inline content
                            // then this may not apply
                            onClosed: function() {
                                $(id).hide();
                            }
                        });
                        break;
                    }
                }
                showfb(location.hash);
                $('a.fblink').click(function(e){
                        showfb($(this).attr('href'));
                });
        });
    </script>
    <style type="text/css" media="screen">
    /* I'm assuming you want the fancybox content to 
    ONLY be displayed in the fancybox. Once again, if using 
    ajax or some other method of getting fancybox content, this might
    not be necessary */
        .fb { display: none; }
    </style>
</head>
<body>

    <p>
        <a class='fblink' href="#fb1">Show 1</a>
    </p>
    <p>
        <a class='fblink' href="#fb2">Show 2</a>
    </p>

    <div id="fb1" class="fb">This is a test</div>
    <div id="fb2" class="fb">This is another test</div>

</body>
</html>

$(函数(){
函数showfb(id){
开关(id){
案例“fb1”:
案例“#fb2”:
//必须这样做,以便fb可以测量内容维度
$(id.show();
$.fancybox({
href:id,
类型:'inline',
//这样一来,内容就不会突然出现在屏幕上
//当然,如果你没有使用内联内容
//那么这可能不适用
onClosed:function(){
$(id).hide();
}
});
打破
}
}
showfb(location.hash);
$('a.fblink')。单击(函数(e){
showfb($(this.attr('href'));
});
});
/*我想你希望fancybox的内容
只能在fancybox中显示。再一次,如果使用
ajax或其他获取fancybox内容的方法,这可能
没必要*/
.fb{显示:无;}

这是一个测试 这是另一个测试
查看这是否有帮助查看这是否有帮助