jQuery(#id).val() vs. getElementById(#id).value

前端 未结 7 949
眼角桃花
眼角桃花 2020-12-01 16:02

I been searching but I can only find articles talking about one or the other. Which one is better?

I\'m making a small web app where performance is not a big concern

7条回答
  •  情书的邮戳
    2020-12-01 16:20

    jQuery does so many nice little error handling things (look below) that I would never write a line of javascript without jquery in a browser again.

    • First, val works on checkbox groups, selects, gets html, and the like.
    • Second, $ lets you use sizzle selectors, so in the future, you can easily switch between an ID and a CSS path.
    • Third, your code will be so much easier to read and maintain if you just use jQuery, that the time you save maintaining your code outweighs any speedup that you admit your users won't see. Finally, jQuery is a very popular, very widely used library. They will make $ and val as fast as they can.

提交回复
热议问题