久草视频2-久草视-久草社区视频-久草色在线-久草色视频-久草软件

jQuery語法對應的DOM API ——選擇元素 – WEB前端開發

我是創始人李巖:很抱歉!給自己產品做個廣告,點擊進來看看。  
英文原文: http://blog.garstasio.com/you-dont-need-jquery/selectors/
愚人碼頭注:

選擇元素

有多少次你看到一個Web應用程序或庫使用jQuery執行簡單瑣碎的元素選擇?有多少次 這樣寫: $(#myElement') ? 或者這樣 $('.myElement') ?噓……你不需要用jQuery選擇元素!這使用DOM API也很容易做到。

  1. IDs
  2. CSS Classes
  3. Tag Names
  4. Attributes
  5. Pseudo-classes
  6. Children
  7. Descendants
  8. Exclusion Selectors
  9. Multiple Selectors
  10. See a Pattern?
  11. Filling in the Gaps
  12. Next in this Series

By ID

jQuery

				// returns a jQuery obj w/ 0-1 elements
				$('#myElement');
			

DOM API

				// IE 5.5+
				document.getElementById('myElement');
			

…或者…

				// IE 8+
				document.querySelector('#myElement');
			

這兩種方法返回一個 Element (元素)。 需要注意的是 使用 getElementById 比使用 querySelector 更高效 。

請問jQuery的語法提供任何好處嗎?我沒有看到一個。你呢?

By CSS Class

jQuery

				// returns a jQuery obj w/ all matching elements
				$('.myElement');
			

DOM API

				// IE 9+
				document.getElementsByClassName('myElement');
			

…或者…

				// IE 8+
				document.querySelectorAll('.myElement');
			

第一個方法返回的 HTMLCollection ,并且 效率最高的是第二個方法 。 querySelectorAll 總是返回一個 NodeList (節點列表) 。

同樣,這里真的很簡單的東西。為什么要使用jQuery?

By Tag Name

舉個例子,選擇頁面上所有的 <div> 元素:

jQuery

$('div');

DOM API

				// IE 5.5+
				document.getElementsByTagName('div');
			

…或者…

				// IE 8+
				document.querySelectorAll('div');
			

正如預期的那樣, querySelectorAll (返回 NodeList )比 getElementsByTagName (返回 HTMLCollection )效率低。

By Attribute(屬性)

選擇所有”data-foo-bar”值為”someval”的元素:

jQuery

$('[data-foo-bar="someval"]');

DOM API

					// IE 8+
					document.querySelectorAll('[data-foo-bar="someval"]');
				

DOM API和jQuery語法非常相似。

By Pseudo-class(偽類)

選擇所有在指定表單中的當前無效(:invalid 偽類)字段。假設我們的表單 ID為”myForm”。

jQuery

$('#myForm :invalid');

DOM API

					// IE 8+
					document.querySelectorAll('#myForm :invalid');
				

Children(子元素)

選擇一個特定元素的所有子元素。?假設我們的特定元素 ID為 “myParent”。

jQuery

$('#myParent').children();

DOM API

					// IE 5.5+
					// NOTE: This will include comment and text nodes as well.
					document.getElementById('myParent').childNodes;
				

…或者…

					// IE 9+ (ignores comment & text nodes).
					document.getElementById('myParent').children;
				

但是,如果我們只想找到特定的子元素呢?比如,有 “ng-click”屬性的子元素?

jQuery

$('#myParent').children('[ng-click]');

…或…

$('#myParent > [ng-click]');

DOM API

					// IE 8+
					document.querySelector('#myParent > [ng-click]');
				

Descendants(后代元素)

找到#myParent下面所有”a”元素。

jQuery

$('#myParent A');

DOM API

					// IE 8+
					document.querySelectorAll('#myParent A');
				

Excluding Elements(排除元素)

選擇所有 <div> 元素,排除那些有”ignore”樣式類 <div> 元素。

jQuery

$('DIV').not('.ignore');

…或者…

$('DIV:not(.ignore)');

DOM API

					// IE 9+
					document.querySelectorAll('DIV:not(.ignore)');
				

Multiple Selectors(多重選擇)

選擇所有 <div> , <a><script> 元素。

jQuery

$('DIV, A, SCRIPT');

DOM API

					// IE 8+
					document.querySelectorAll('DIV, A, SCRIPT');
				

See a Pattern?

如果我們專注于選擇器的支持,并且不需要處理IE8以下的瀏覽器,我們只需用這個替代jQuery:

					window.$ = function(selector) {
					var selectorType = 'querySelectorAll';
					if (selector.indexOf('#') === 0) {
					selectorType = 'getElementById';
					selector = selector.substr(1, selector.length);
					}
					return document[selectorType](selector);
					};
				

But I Want More!

對于絕大多數 項目中,選擇器支持到Web API就足夠了。但是,如果你不幸需要支持IE7?在這種情況下,你可能需要一些第三方的代碼來提供一些幫助。

當然,你僅僅需要引入jQuery,但當你只需要支持現在先進的選擇器時,為什么用這么大的代碼庫呢?相反,嘗試一下micro-library(微小的庫)完全專注于元素選擇??紤], Sizzle ,這恰好是jQuery使用的選擇庫。 Selectivizr 是另一種非常小的選擇庫,在很老的瀏覽器上也能支持CSS3選擇器。

Next

下一篇:操作DOM元素,敬請期待!

本文被轉載1次

首發媒體 Web前端開發 | 轉發媒體

隨意打賞

提交建議
微信掃一掃,分享給好友吧。
主站蜘蛛池模板: 成人在线视频观看 | 国产毛片在线观看 | 99精品久久99久久久久久 | 四虎影视4hu最新地址在线884 | www久久com| 国产午夜久久精品 | 2021精品国夜夜天天拍拍 | 精品亚洲欧美中文字幕在线看 | 兽皇videos日本另类 | 任我鲁精品视频精品 | 耽美调教高h | 国产在线精品亚洲第一区香蕉 | 免费观看视频在线播放 | 免费人成黄页在线观看69 | 亚洲狠狠婷婷综合久久久久网站 | 日本高清视频在线观看 | 99视频网址 | 国色天香社区视频免费观看3 | 欧美成a人片免费看久久 | 国产亚洲精品综合在线网址 | 成人免费观看一区二区 | 成人免费一区二区三区在线观看 | 午夜国产福利视频一区 | 日本不卡1卡2卡三卡网站二百 | 香蕉在线精品一区二区 | 91在线精品国产 | 2021国产麻豆剧传媒剧情动漫 | 欧美一级专区免费大片俄罗斯 | 精品在线视频一区 | 亚洲精品一区制服丝袜 | 国产在线观看99 | 国产成人99精品免费观看 | 天堂伊人网 | 6080欧美一区二区三区四区 | 久久中文字幕无线观看 | fc2免费人成在线 | 99热精品在线播放 | 香蕉久久网 | 99久久久久久久 | 国产精品夜色视频一级区 | 日本在线亚州精品视频在线 |