admin管理员组

文章数量:1332395

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.

本文标签: javascriptWhy change from focus() to trigger(39focus39)Stack Overflow