When to use Vanilla JavaScript vs. jQuery?(何时使用 Vanilla 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.
A specific example
$(this) vs this
Inside a click event referencing the clicked objects id
jQuery
$(this).attr("id");
Javascript
this.id;
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)
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(as you know)this.value(on most input types. only issues I know are IE when a<select>doesn't havevalueproperties set on its<option>elements, or radio inputs in Safari.)this.classNameto get or set an entire "class" propertythis.selectedIndexagainst a<select>to get the selected indexthis.optionsagainst a<select>to get a list of<option>elementsthis.textagainst an<option>to get its text contentthis.rowsagainst a<table>to get a collection of<tr>elementsthis.cellsagainst a<tr>to get its cells (td & th)this.parentNodeto get a direct parentthis.checkedto get the checked state of acheckboxThanks @Tim Downthis.selectedto get the selected state of anoptionThanks @Tim Downthis.disabledto get the disabled state of aninputThanks @Tim Downthis.readOnlyto get the readOnly state of aninputThanks @Tim Downthis.hrefagainst an<a>element to get itshrefthis.hostnameagainst an<a>element to get the domain of itshrefthis.pathnameagainst an<a>element to get the path of itshrefthis.searchagainst an<a>element to get the querystring of itshrefthis.srcagainst an element where it is valid to have asrc
...I think you get the idea.
There will be times when performance is crucial. Like if you're performing something in a loop many times over, you may want to ditch jQuery.
In general you can replace:
$(el).attr('someName');
with:
Above was poorly worded. getAttribute is not a replacement, but it does retrieve the value of an attribute sent from the server, and its corresponding setAttribute will set it. Necessary in some cases.
The sentences below sort of covered it. See this answer for a better treatment.
el.getAttribute('someName');
...in order to access an attribute directly. Note that attributes are not the same as properties (though they mirror each other sometimes). Of course there's setAttribute too.
Say you had a situation where received a page where you need to unwrap all tags of a certain type. It is short and easy with jQuery:
$('span').unwrap(); // unwrap all span elements
But if there are many, you may want to do a little native DOM API:
var spans = document.getElementsByTagName('span');
while( spans[0] ) {
var parent = spans[0].parentNode;
while( spans[0].firstChild ) {
parent.insertBefore( spans[0].firstChild, spans[0]);
}
parent.removeChild( spans[0] );
}
This code is pretty short, it performs better than the jQuery version, and can easily be made into a reusable function in your personal library.
It may seem like I have an infinite loop with the outer while because of while(spans[0]), but because we're dealing with a "live list" it gets updated when we do the parent.removeChild(span[0]);. This is a pretty nifty feature that we miss out on when working with an Array (or Array-like object) instead.
这篇关于何时使用 Vanilla JavaScript 与 jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:何时使用 Vanilla JavaScript 与 jQuery?
基础教程推荐
- 检查 HTML5 拖放文件类型 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
