When to use Vanilla JavaScript vs. jQuery?

后端 未结 13 2076
独厮守ぢ
独厮守ぢ 2020-11-22 09:08

I have noticed while monitoring/attempting to answer common jQuery questions, that there are certain practices using javascript, instead of jQuery, that actually enable you

13条回答
  •  一个人的身影
    2020-11-22 09:37

    When:

    1. you know that there is unflinching cross-browser support for what you are doing, and
    2. it is not significantly more code to type, and
    3. it is not significantly less readable, and
    4. you are reasonably confident that jQuery will not choose different implementations based on the browser to achieve better performance, then:

    use JavaScript. Otherwise use jQuery (if you can).

    Edit: This answer applies both when choosing to use jQuery overall versus leaving it out, as well as choosing whether to to use vanilla JS inside jQuery. Choosing between attr('id') and .id leans in favor of JS, while choosing between removeClass('foo') versus .className = .className.replace( new Regexp("(?:^|\\s+)"+foo+"(?:\\s+|$)",'g'), '' ) leans in favor of jQuery.

提交回复
热议问题