javascript - Why change from focus() to trigger('focus')? - Stack Overflow

I was going through the history of dropdown.js in bootstrap and came across the following change Histor

I was going through the history of dropdown.js in bootstrap and came across the following change History file on git, here:

$this.focus()   // focus() being changed to trigger('focus');
$this.trigger('focus')

Now, the guy here has left a ment about the change saying:

Makes life for people with custom jQuery builds excluding event aliases much easier.

I don't quite understand what's the difference here between using focus() or trigger('focus'), as for me both have the same effect; why has the author chosen such a change?

I was going through the history of dropdown.js in bootstrap and came across the following change History file on git, here:

$this.focus()   // focus() being changed to trigger('focus');
$this.trigger('focus')

Now, the guy here has left a ment about the change saying:

Makes life for people with custom jQuery builds excluding event aliases much easier.

I don't quite understand what's the difference here between using focus() or trigger('focus'), as for me both have the same effect; why has the author chosen such a change?

Share Improve this question edited Sep 24, 2018 at 17:12 BenMorel 36.6k51 gold badges205 silver badges336 bronze badges asked Apr 7, 2015 at 22:20 Alexander SolonikAlexander Solonik 10.3k19 gold badges86 silver badges185 bronze badges 2
  • 3 huh , why the downvote ?? – Alexander Solonik Commented Apr 7, 2015 at 22:25
  • i am here to spead love nd upvotes ... and brotherhood , haters gonna hate . – Alexander Solonik Commented Apr 7, 2015 at 22:34
Add a ment  | 

2 Answers 2

Reset to default 6

https://github./jquery/jquery#modules.

If you are making a custom jQuery build and exclude event/alias module - you won't have shortcuts to events (e.g. .click(), .focus(), .ready(), etc).

So you'll have to use .on('eventName', handler) for event binding and consequently .trigger('eventName') to trigger jQuery event.

People with custom jQuery builds could create or modify their own focus() functions intended to do whatever they want. Imagine if you create your own focus() which allows lot of parameters and chains multiple callback; it would be a mess when you bine its usage with the jQuery focus() basic function.

When you use a trigger it's qute obvious you're going to trigger an action; in this case, to focus an element.

Beside of that, using the trigger() functions makes the code a little easier to understand.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742283417a4414907.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信