Javascript 将数据属性从锚元素传递到jQuery函数

Javascript 将数据属性从锚元素传递到jQuery函数,javascript,jquery,Javascript,Jquery,我有一个需要在弹出窗口中打开的共享图标和URL列表,例如:facebook、twitter等 我有一个jQuery popupWindow函数,我不记得在哪里找到它,但它采用以下设置列表: $.fn.popupWindow.defaultSettings = { centerBrowser:0, centerScreen:0, height:500, left:0, location:0, menubar:0, resizable:0,

我有一个需要在弹出窗口中打开的共享图标和URL列表,例如:facebook、twitter等

我有一个jQuery popupWindow函数,我不记得在哪里找到它,但它采用以下设置列表:

$.fn.popupWindow.defaultSettings = {
    centerBrowser:0,
    centerScreen:0,
    height:500,
    left:0,
    location:0,
    menubar:0,
    resizable:0,
    scrollbars:0,
    status:0,
    width:500,
    windowName:null,
    windowURL:null,
    top:0,
    toolbar:0
};
因此,我只需输入高度、宽度和中心,就可以得到一个非常好的弹出窗口,供分享,如下所示:

$(element).popupWindow({ 
    centerBrowser: 1,
    width: 500,
    height: 250 
});
我遇到的问题是,我无法访问链接的数据属性,以便将高度和宽度传递给popover方法

我的链接类似于:

<a href="http://www.facebook.com/sharer.php?etc" 
    class="social-share facebook" 
    data-width="550" 
    data-height="275">Share on Facebook</a>
这将忽略脚本,并在同一窗口中转到FB

尝试:

$('.social-share').popupWindow({ 
    centerBrowser: 1,
    height: $(this).attr('data-height'),
    width: $(this).attr('data-width')
});
$('.social-share').popupWindow({ 
    centerBrowser: 1,
    height: this.attr('data-height'),
    width: this.attr('data-width')
});
对象没有属性attr

在popupWindow函数中,他们编写了一些重写,这些重写首先选择元素的属性,然后回退到如下设置:

settings.windowName = this.name || settings.windowName;
settings.windowURL = this.href || settings.windowURL;
我的想法是,这是最优雅、最实用的解决方案,因此会有三个回退,首先查看是否有数据宽度和数据高度属性,然后检查传入的设置,然后使用默认设置

但是,唉,我似乎也无法让它发挥作用

我尝试添加两个:

settings.width = $(this).attr('data-width') || settings.width;
settings.height = $(this).attr('data-height') || settings.height;
这对高度或宽度没有影响,只使用默认值

并尝试:

settings.width = this.attr('data-width') || settings.width;
settings.height = this.attr('data-height') || settings.height;
此抛出错误对象没有属性attr

救命啊

试试这个 不要频繁地访问DOM,而是尝试使用.data进行访问 我想问题是你没有增加单位


对象没有属性数据。另外,popupWindow函数在设置中不采用单位。
$('.social-share').popupWindow({ 
    centerBrowser: 1,
    height: this.data('height') + 'px',
    width:  this.data('width') + 'px'
});