问题
.toggle( handler, handler [, handler ] )
I believe this API is very handy. But why jQuery 1.9 removed it?
回答1:
It was deprecated in 1.8, as per ticket # 11786. It was later removed in 1.9.
Basically, that ticket contains the real reasons, such as:
The
.toggle()
method is provided for convenience. It is relatively straightforward to implement the same behavior by hand, and this can be necessary if the assumptions built into.toggle()
prove limiting.For example,
.toggle()
is not guaranteed to work correctly if applied twice to the same element.Since
.toggle()
internally uses a click handler to do its work, we must unbind click to remove a behavior attached with.toggle()
, so other click handlers can be caught in the crossfire.The implementation also calls
.preventDefault()
on the event, so links will not be followed and buttons will not be clicked if.toggle()
has been called on the element.
来源:https://stackoverflow.com/questions/25052129/why-did-jquery-remove-toggle-method