繁体   English   中英

在$ .fn中使用jquery选项会导致错误

[英]Using jquery options with $.fn causes errors

在插件(jquery)的一部分中,我将CSS规则添加到元素中。 这是代码:

tagSet.eq(index).css({
                    options.tag_direction : (xy[0]+"px "+strictCSS),
                    "position" : ("relative"+" "+strictCSS),

                    "top" : (xy[1]+"px "+strictCSS),
                })

但是,如果我用options.get_direction_hor替换“ left”,它会出错。 这是错误:

SyntaxError: missing : after property id


options.tag_direction_hor : (xy[0]+"px "+strictCSS),

之所以这样做,是因为我希望用户(开发人员)能够使用带有“ left”或“ right” css规则的插件,从而避免黑客入侵源代码。

我也有一个console.log(options.get_direction_hor) ,它输出正确。 我在这里缺少关于javascript对象的任何信息吗?

这是选项签名:

$.fn.mtTagger = function(options){
        var options = $.extend({
            tag_class : ".mtTagElement", //
            tag_before_callback : null,  // a callback to gets fired when the pages(or section) loads       
            tag_htmlTag : "span", // HTML elements to be selected as children           
            tag_limit_to_class : false, // if true, then the selection is $(element.class) than it was $(element) if false
            tag_mouse_in_callback : null, // a callback to gets fired when the mouse moves enters
            tag_mouse_out_callback : null, // a callback to gets fired when the mouse moves leaves
            tag_strict_css : false, // if true, then !important will be appened to the end of each CSS rule
            tag_direction_hor : null
        }, options)

您的语法不正确。 对象文字定义不能包含动态键,就像您尝试提供options.tag_direction 相反,您可以将其分为两部分:

var styles = {
    position: "relative" + " " + strictCSS,
    top: xy[1] + "px " + strictCSS
};
styles[options.tag_direction] = xy[0] + "px " + strictCSS;
tagSet.eq(index).css(styles);

还记得在最后一个对象属性之后清理尾随逗号(在旧的IE中会中断)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM