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?
- 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
2 Answers
Reset to default 6https://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条)