2015-09-25 31 views
0

我有這個jQuery代碼,我想單獨工作罰款。我的問題是,有沒有一種方法可以將當時的情況作爲一個整體繼續工作?如何將兩個事件合併到選擇器?

$('#truck_brand').poshytip({className: 'tip-yellowsimple', showOn: 'focus', alignTo: 'target', alignX: 'right', alignY: 'center', offsetX: 5}); 
$('#truck_brand').focus(function() { $('#brand_error').poshytip('hide'); }); 
+0

你沒有使用兩個事件,你正在使用一個事件處理程序('focus')和一個看起來像處理一個工具提示的方法。你無法在沒有訪問源代碼的情況下合併這些代碼,而且在任何情況下你都不希望它們完成不同的事情。 – Palpatim

+0

把它們放在一起是什麼意思?像一個方法鏈?見http://schier.co/blog/2013/11/14/method-chaining-in-javascript.html – wiesion

+0

@Palpatim很好,確認兩者都不能作爲一個人工作 – Maa

回答

0

如何約:

$('#truck_brand').poshytip({className: 'tip-yellowsimple', showOn: 'focus', alignTo: 'target', alignX: 'right', alignY: 'center', offsetX: 5}).focus(function() { $('#brand_error').poshytip('hide'); }); 
+0

你的代碼是一條生命線。大!!!有效。 – Maa

0

你總是可以緩存一個或多個DOM元素的jQuery的實例:

var $truckBrand = $('#truck_brand'); 
$truckBrand.poshytip({className: 'tip-yellowsimple', showOn: 'focus', alignTo: 'target', alignX: 'right', alignY: 'center', offsetX: 5}); 
$truckBrand.focus(function() { $('#brand_error').poshytip('hide'); }); 

.focus,您還可以鏈到jQuery的實例,再調用:

$('#truck_brand') 
    .focus(function() { $('#brand_error').poshytip('hide'); }) 
    .poshytip({className: 'tip-yellowsimple', showOn: 'focus', alignTo: 'target', alignX: 'right', alignY: 'center', offsetX: 5}); 
相關問題