欧美中文字幕第一页-欧美中文字幕一区-欧美中文字幕一区二区三区-欧美中文字幕在线-欧美中文字幕在线播放-欧美中文字幕在线视频

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前端開發 | 轉發媒體

隨意打賞

提交建議
微信掃一掃,分享給好友吧。
主站蜘蛛池模板: 深夜福利视频在线一区 | 高清在线亚洲精品国产二区 | 97视频网站 | 久久精品国产亚洲aa | 天天操天天操天天操 | 色网站在线看 | 东北老妇露脸xxxxx | 国产成人精品一区二区免费 | 91综合久久婷婷久久 | 中文国产成人精品少久久 | 私人影院在线免费观看 | 久久久精品免费国产四虎 | 欧美高清无砖专区欧美精品 | 亚洲日本一区二区三区高清在线 | 久久综合久久网 | 亚洲精品色综合色在线观看 | 欧美激情_区二区三区 | 欧美天天影院 | 一级毛片老太婆交性欧美 | 亚洲精品国产专区一区 | 久久精品国产这里是免费 | 久久久精品视频在线观看 | 男女性高清爱潮视频免费观看 | 国产成人精品综合网站 | 豆国产97在线 | 亚洲 | 久久精品免费视频6 | 久久久中文字幕日本 | 国产九九热视频 | 久久伊人热 | 欧美毛片免费看 | 欧美亚洲国产成人高清在线 | 日韩 三级 | 久久99精品久久久久久园产越南 | 久久精品香蕉 | 乱子伦xxxx厨房 | 国内精品久久久久影院网站 | 亚洲欧美另类国产综合 | 99久久综合给久久精品 | 精品精品国产自在香蕉网 | 99久久精品毛片免费播放 | 久热免费 |