久草视频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前端開發 | 轉發媒體

隨意打賞

提交建議
微信掃一掃,分享給好友吧。
主站蜘蛛池模板: 91久久国产露脸精品 | 男生操女生的漫画 | 短篇最污的乱淫伦小说全集 | 欧美在线视频一区 | 亚洲精品在看在线观看 | 明星ai人脸替换脸忘忧草 | 成年人视频在线 | 亚洲精品国产AV成人毛片 | 欧美日韩人成在线观看 | 久草在线福利资站免费视频 | 国产成人一区二区三区小说 | 蜜桃影像传媒破解版 | 国产成人激情 | 亚洲日日操| 亚洲一区二区精品推荐 | 亭亭色| 欧美精品99久久久久久人 | 欧美在线视频免费播放 | 亚洲国产日韩成人综合天堂 | 毛片www| 国产精品亚洲午夜一区二区三区 | 四虎影视免费观看 | 亚洲七七久久综合桃花 | 欧美激烈精交gif动态图18p | 欧洲喷浆乌克兰 | 深夜免费在线视频 | 久久水蜜桃亚洲AV无码精品偷窥 | 久久er99热精品一区二区 | 调教女高中生第3部分 | 双性人bbww欧美双性 | 全黄h全肉细节文在线观看 全彩成人18h漫画 | 国产精品乱码高清在线观看 | 俺去俺去啦最新官网在线 | 亚洲福利视频在线观看 | 亚洲国产精品自产在线播放 | 精品久久免费观看 | 欧美一区精品二区三区 | 亚洲精品一区在线观看 | 五月色婷婷在线影院 | 日日操美女| coolgay男男gayxxx chinese壮直男gay老年人 chinese野外gay军人 |