2015-09-25 53 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}); 
相关问题