javascript 从函数中使用 iframe 打开fancybox 弹出窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14521803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:50:58  来源:igfitidea点击:

Open fancybox popup with iframe from a function

javascriptjqueryfancyboxlightbox

提问by Raja

I have searched enough in the Stackoverflow and i could not able to find a thread to answer my question.

我已经在 Stackoverflow 中进行了足够的搜索,但找不到可以回答我的问题的线程。

Below is my scenario,

下面是我的场景

function openVideo(videoid,urlid){
        var videourl = 'http://localhost/ptchoice-local/welcome/video/'+videoid+'/'+urlid;

        $("a#video_link").fancybox({
                    'type': 'iframe', 
            'width' : 1000,
            'height' : 600,
            'autoDimensions' : false,
            'autoScale' : false,
            'href' : videourl
        });

    $("a#video_link").trigger('click');
    return false;
} 

<a href="#" onclick="openVideo('2134sdf234532sdfs324234','1')">Show video</a>

So, i would like to set href value when i click the "openVideo" function not in the anchor tag itself.

因此,当我单击不在锚标记本身中的“openVideo”功能时,我想设置 href 值。

Fancybox version: jquery.fancybox-1.3.4.pack.js

Fancybox 版本:jquery.fancybox-1.3.4.pack.js

Please suggest on this.

请就此提出建议。

回答by areich

If I understood your question correctly this should work for you:

如果我正确理解了您的问题,这应该对您有用:

Need to add an ID to the anchor:

需要给anchor添加一个ID:

<a href="#" id="showVideo">Show video</a>

The jQuery:

jQuery:

$("#showVideo").click(function () {
    var videourl = 'http://localhost/ptchoice-local/welcome/video/2134sdf234532sdfs324234/1';

    $("#video_link").fancybox({
        'type': 'iframe', 
        'width' : 1000,
        'height' : 600,
        'autoDimensions' : false,
        'autoScale' : false,
        'href' : videourl
    });

    $("#video_link").trigger('click');
});

Since the videoid and urlid were hard coded in your example, I did the same here. They can be variables if they need be -- just set them the same way.

由于 videoid 和 urlid 在您的示例中是硬编码的,我在这里做了同样的事情。如果需要,它们可以是变量——只需以相同的方式设置它们。

回答by evilom

You can do it this way.

你可以这样做。

function openVideo(videoid,urlid) {
    var videourl = 'http://localhost/ptchoice-local/welcome/video/'+videoid+'/'+urlid;
    $.fancybox.open({
        href: videourl,
        type: 'iframe',
        padding: 0
    });
}