javascript - jQuery:如何从 "this"对象获取值?

标签 javascript jquery

我制作了一个帮助程序,用于在单击时打开新的弹出窗口,但我在对象内设置默认值时遇到问题。我需要计算弹出窗口的顶部和左侧位置以使新弹出窗口居中。完整代码如下:

/*
    $(element).onPopup(options); - Open Popup window
    -Ths function open new popup window on your browser

    EXAMPLE:
    ----------------------------------------------
    <a href="http://google.com">Google</a>

    $("a#link").onPopup({
        name        :   "Popup Window",
        width       :   800,
        height      :   600
    });

    OPTIONS:
    ----------------------------------------------
    attr            // attribute where is located link
    name            // name of popup window
    width           // max width
    height          // max height
    left            // position left    (px)
    top             // position top (px)
    resizable       // resizable 1 or 0
    location        // display location 1 or 0
    fullscreen      // open in full screen  1 or 0
    scrollbars      // display scroll bars  1 or 0
    titlebar        // display title bar    1 or 0
    toolbar         // display tool bar     1 or 0
    directories     // display directories  1 or 0
*/
$.fn.onPopup=function(options){
    var s   = {
            attr        :   "href",
            name        :   "Popup Window",
            width       :   700,
            height      :   600,
            left        :   ($(window).width()/2)-(this.width/2),
            top         :   ($(window).height()/2)-(this.height/2),
            resizable   :   0,
            location    :   0,
            fullscreen  :   0,
            scrollbars  :   1,
            titlebar    :   0,
            toolbar     :   0,
            directories :   0
        },
        $element    = this;

    s = $.extend(s,options);

    $element.on("click",function(e) {
        e.stopPropagation(); e.preventDefault();
        window.open(
            $(this).attr(s.attr), s.name, "width="+s.width+", height="+s.height+", directories="+s.directories+", toolbar="+s.toolbar+", titlebar="+s.titlebar+", scrollbars="+s.scrollbars+", fullscreen="+s.fullscreen+", location="+s.location+", resizable="+s.resizable+", top="+s.top+", left="+s.left
        );
     });
};

这就是我的问题所在:

var s   = {
             /*...*/

                width       :   700,
                height      :   600,
                left        :   ($(window).width()/2)-(this.width/2),
                top         :   ($(window).height()/2)-(this.height/2),

             /*...*/

            },

如何将宽度/高度传递给另一个对象来工作?

最佳答案

一种方法是首先创建对象,然后添加引用对象中其他属性的额外属性

var s   = {
        attr        :   "href",
        name        :   "Popup Window",
        width       :   700,
        height      :   600,
        left        :   null, //calculated if not user provided
        top         :   null  //calculated if not user provided
       ....
    };

// update user settings
s = $.extend(s,options);

// calculate based on actual values
if(s.left === null){
   s.left =  ($(window).width()/2)-(s.width/2);
}
if(s.top === null){
   s.top  =  ($(window).height()/2)-(s.height/2);
}

另请注意,您应该返回 this.each(.. 并在那里运行您的业务,这样当选择器包含多个元素时您就有单独的实例,并使插件可与其他 jQuery 方法链接

关于javascript - jQuery:如何从 "this"对象获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487121/

相关文章:

javascript - 检查条件是否与 Javascript 中数组的任何值匹配

javascript - 无限循环而不是移动到下一个文件夹

jQuery:从文件系统读取文本文件

javascript - contenteditable div 中的 n gram 突出显示

javascript - 限制数量并限制为两个

javascript - 如何在 Javascript 中动态使用子对象值查找父 JSON 对象值?

javascript - 将三个数组转换为嵌套对象

javascript - 如何仅更改 html 表格中最小单元格的颜色

javascript - JSF/PrimeFaces ajax 更新打破了 jQuery 事件监听器函数绑定(bind)

javascript - 文档头部的 JQuery 和脚本