什么时候使用 Vanilla JavaScript 和 jQuery? [英] When to use Vanilla JavaScript vs. jQuery?

查看:22
本文介绍了什么时候使用 Vanilla JavaScript 和 jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在监控/尝试回答常见的 jQuery 问题时注意到,有一些使用 javascript 而不是 jQuery 的做法,实际上使您能够少写多做……同样如此数量.并且还可能产生性能优势.

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 to write less and do ... well the same amount. And may also yield performance benefits.

具体例子

$(this) vs this

在引用被点击对象 id 的点击事件中

Inside a click event referencing the clicked objects id

jQuery

$(this).attr("id");

Javascript

this.id;

还有其他类似的常见做法吗?某些 Javascript 操作可以更轻松地完成,而无需将 jQuery 混入其中.或者这是一个罕见的案例?(实际上需要更多代码的jQuery快捷方式")

Are there any other common practices like this? Where certain Javascript operations could be accomplished easier, without bringing jQuery into the mix. Or is this a rare case? (of a jQuery "shortcut" actually requiring more code)

虽然我很欣赏有关 jQuery 与普通 javascript 性能的答案,但实际上我正在寻找更多定量的答案.在使用 jQuery 时,使用纯 javascript 而不是使用 $() 实际上会更好(可读性/紧凑性)的实例.除了我在原始问题中给出的示例之外.

EDIT : While I appreciate the answers regarding jQuery vs. plain javascript performance, I am actually looking for much more quantitative answers. While using jQuery, instances where one would actually be better off (readability/compactness) to use plain javascript instead of using $(). In addition to the example I gave in my original question.

推荐答案

  • this.id(如您所知)
  • this.value(在大多数输入类型上.当 获取所选索引
  • this.options 针对
发送“验证码”获取 | 15天全站免登陆