如何在jQuery函数中使用options var?

时间:2017-01-08 12:35:22

标签: javascript jquery html

我有一个名为 jQuery DOM Line 的脚本,我想使用它。它允许我通过插入我的vars在div之间画一条线。作者告诉我使用它:

$.line(fromPoint, toPoint[, options]);

其中一个选项名为className,我想用它来向.blue添加额外的.jquery-line类,所以所有创建的div都看起来像{{1} }}

默认选项如下所示:

<div class="jquery-line blue">

我画这样的一行:

$.line.defaults = {
    className: 'jquery-line',
    lineWidth: 1,
    lineColor: '#000',
    returnValues: false
};

如何添加额外的className?我尝试过以下内容:

var firstDot = $(this);
var secondDot = dots.eq( currentIndex + 1 + offset);
firstDot.y = firstDot.offset().top;
firstDot.x = firstDot.offset().left;
secondDot.y = secondDot.offset().top;
secondDot.x = secondDot.offset().left;
$.line(firstDot, secondDot);

哪个不起作用。由于我不熟悉他正在使用的语法,有人可以帮助我吗?我知道这一定很容易......

2 个答案:

答案 0 :(得分:1)

该插件要求JavaScript Object所以不要传递array,而是像现在这样做,传递一个这样的对象:

$.line(firstDot, secondDot, {className: "blue"});

或:

var myProperties = {className: "blue", lineWidth: 3, lineColor: "pink"};

$.line(firstDot, secondDot, myProperties);

答案 1 :(得分:1)

方括号表示参数是可选的,所以

 $.line(fromPoint, toPoint[, options]);

表示您可以像

一样调用该函数
 $.line(firstDot, secondDot);

或喜欢

 $.line(firstDot, secondDot, options);

您应该尝试这样称呼它:

 $.line(firstDot, secondDot, {className: 'jquery-line blue'})

另请注意,该软件包包含lineColor的选项 - 因此您可能需要这样调用它;

 $.line(firstDot, secondDot, {lineColor: 'blue'})