javascript - 使用 ID 调用函数,更改为 onclick?

标签 javascript jquery

我有一个覆盖脚本,当链接如下时会弹出:

<a id="b1" href="page.html">link</a>

使用了

。问题是我需要多个链接,但不能使用相同的 ID。

这是代码:

$(function(){

$('#b1').frameWarp();

});

我尝试用 .b1 和 a.b1 替换 #b1 并使用类而不是 ID。这在第一个链接上工作正常,但单击任何后续链接都会导致单击第一页再次打开。我认为与覆盖脚本中已使用的类存在冲突。

有没有办法可以使用 onclick 来使其以同样的方式工作?

该脚本来自插件,但以下是其余部分:

(function($){

// Private varialble deffinitions

var body = $('body'),
    win = $(window),
    popup, popupBG;

var frameCache = {};
var frameCacheDiv = $('<div class="frameCacheDiv">').appendTo('body');
var currentIframe;

// The main plugin code

$.fn.frameWarp = function(settings){

    // Supplying default settings

    settings = $.extend({
        cache: false,
        url: '',
        width:600,
        height:500,
        closeOnBackgroundClick: true,
        onMessage:function(){},
        onShow:function(){}
    }, settings);

    this.on('click',function(e){

        e.preventDefault();

        var elem = $(this),
            offset = elem.offset();

        // The center of the button
        var buttonCenter = {
            x: offset.left - win.scrollLeft() + elem.outerWidth()/2,
            y: offset.top - win.scrollTop() + elem.outerHeight()/2
        };

        // The center of the window
        var windowCenter = {
            x: win.width()/2,
            y: win.height()/2
        };

        // If no URL is specified, use the href attribute.
        // This is useful for progressively enhancing links.

        if(!settings.url && elem.attr('href')){
            settings.url = elem.attr('href');
        }

        // The dark background

        popupBG = $('<div>',{'class':'popupBG'}).appendTo(body);

        popupBG.click(function(){

            if(settings.closeOnBackgroundClick){
                hide();
            }

        }).animate({    // jQuery++ CSS3 animation
            'opacity':1
        },400);


        // The popup

        popup = $('<div>').addClass('popup').css({
            width   : 0,
            height  : 0,
            top     : buttonCenter.y,
            left    : buttonCenter.x - 35
        });

        // Append it to the page, and trigger a CSS3 animation
        popup.appendTo(body).animate({
            'width'                 : settings.width,
            'top'                   : windowCenter.y - settings.height/2,
            'left'                  : windowCenter.x - settings.width/2,
            'border-top-width'      : settings.height,
            'border-right-width'    : 0,
            'border-left-width'     : 0
        },200,function(){

            popup.addClass('loading').css({
                'width': settings.width,
                'height': settings.height
            });

            var iframe;

            // If this iframe already exists in the cache
            if(settings.cache && settings.url in frameCache){
                iframe = frameCache[settings.url].show();
            }
            else{

                iframe = $('<iframe>',{
                    'src' : settings.url,
                    'css' : {
                        'width' : settings.width,
                        'height' : settings.height,
                    }
                });

                // If the cache is enabled, add the frame to it
                if(settings.cache){
                    frameCache[settings.url] = iframe;
                    iframe.data('cached',true);
                    settings.onShow();
                }
                else{

                    // remove non-cached iframes
                    frameCacheDiv.find('iframe').each(function(){
                        var f = $(this);
                        if(!f.data('cached')){
                            f.remove();
                        }
                    });
                }

                iframe.ready(function(){
                    frameCacheDiv.append(iframe);
                    setUpAPI(iframe, settings);
                    settings.onShow();
                });
            }

            currentIframe = iframe;

        });

    });

    return this;
};

// Helper Functions

function hide(){

    if(currentIframe){
        currentIframe.hide();
        currentIframe = null;
    }

    popupBG.remove();
    popup.remove();
}

function setUpAPI(iframe, settings){

    if(sameOrigin(settings.url)){

        // Exposing a minimal API to the iframe
        iframe[0].contentWindow.frameWarp = {
            hide: hide,
            sendMessage:function(param){
                return settings.onMessage(param);
            }
        };
    }
}

function sameOrigin(url){

    // Compare whether the url belongs to the
    // local site or is remote

    return (getOrigin(url) == getOrigin(location.href));
}

function getOrigin(url){

    // Using an anchor element to
    // parse the URL

    var a = document.createElement('a');
    a.href = url;

    return a.protocol+'//'+a.hostname;
}

})(jQuery);

最佳答案

试试这个:

<a href="javascript:this.onclick=framewrap()">link1</a>
<a href="javascript:this.onclick=framewrap()">link2</a>
<a href="javascript:this.onclick=framewrap()">link3</a>

没试过,但应该可以

关于javascript - 使用 ID 调用函数,更改为 onclick?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11781640/

相关文章:

php - parsererror : SyntaxError: JSON. 解析:JSON 数据第 2 行第 1 列出现意外字符 200 OK

javascript - 如何将 IndexedDB 表数据检索到变量?

javascript - jQuery加载导致body变白

javascript - 在 webgl 和 opengles 中渲染带有反射的三 Angular 形网格的问题

javascript - jQuery 使图像弹出在随机位置不起作用

javascript - 在没有配置文件的情况下从 JS 运行 karma 测试

jquery - 在点击事件中使用 stopPropagation

jquery - 单击时加载带参数的脚本

JavaScript 无法在 IE、.data 中运行?设置间隔?

javascript - 在 DevTools 中检查 ImmutableJS 对象