What is best way to perform jQuery .change()

后端 未结 1 2019
深忆病人
深忆病人 2020-12-06 12:19

I have 2 separate scripts that essentially do the same thing. I built them over time and just discovered I am using a couple different means to get to the same result.

相关标签:
1条回答
  • 2020-12-06 13:09

    Before jQuery 1.7, change() was simply a short cut for bind("change").

    As of 1.7 however, on() was introduced, and is preferred to bind(). That now means change() is a shortcut for on("change"), and in fact all bind() calls will now call on() internally.

    In short, they do the same thing. I find the explicit use of on() (or bind()) preferable, but as long as you're consistent throughout your code base, I don't see any real differences.

    One could argue that using change() over on("change") is "better", as a typo in the word "change" would throw a parse error in the first instance ("undefined is not a function"), but would fail silently with on()... but obviously your unit tests would catch that, right? ;).

    0 讨论(0)
提交回复
热议问题