2017-01-08 30 views
2

我收到了一個腳本,叫做jQuery DOM Line,我想用。它允許我通過插入我的變量來在div之間畫一條線。作者告訴我這樣使用它:如何在jQuery函數中使用選項var?

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

其中一個選項叫做className,我想用一個額外.blue類添加到.jquery-line,所以一切的一切所創建的DIV的樣子<div class="jquery-line blue">

默認選項是這樣的:

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

我畫我的路線是這樣的:

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); 

我如何添加額外的className?我嘗試過的一些事情,如:

$.line(firstDot, secondDot, [className: 'blue']); 

不工作。由於我不習慣他正在使用的語法,有人可以幫我嗎?我知道它必須是東西容易...

回答

1

該插件要求一個JavaScript Object如此,而不是傳遞array的,因爲你現在正在做向右轉,經過一個對象是這樣的:

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

或:

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

$.line(firstDot, secondDot, myProperties); 
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'}) 
相關問題