欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery選擇器querySelector的使用指南

 更新時間:2015年01月23日 11:29:49   投稿:hebedich  
這篇文章主要介紹了jQuery選擇器querySelector的使用指南的相關(guān)資料,需要的朋友可以參考下

簡介

HTML5向Web API新引入了document.querySelector以及document.querySelectorAll兩個方法用來更方便地從DOM選取元素,功能類似于jQuery的選擇器。這使得在編寫原生JavaScript代碼時方便了許多。
用法

兩個方法使用差不多的語法,都是接收一個字符串參數(shù),這個參數(shù)需要是合法的CSS選擇語法。

復(fù)制代碼 代碼如下:

element = document.querySelector('selectors');
elementList = document.querySelectorAll('selectors');

其中參數(shù)selectors 可以包含多個CSS選擇器,用逗號隔開。

復(fù)制代碼 代碼如下:

element = document.querySelector('selector1,selector2,...');
elementList = document.querySelectorAll('selector1,selector2,...');

使用這兩個方法無法查找?guī)晤悹顟B(tài)的元素,比如querySelector(':hover')不會得到預(yù)期結(jié)果。

querySelector

復(fù)制代碼 代碼如下:

element = document.querySelector('div#container');//返回id為container的首個div
element = document.querySelector('.foo,.bar');//返回帶有foo或者bar樣式類的首個元素

querySelectorAll

該方法返回所有滿足條件的元素,結(jié)果是個nodeList集合。查找規(guī)則與前面所述一樣。

elements = document.querySelectorAll('div.foo');//返回所有帶foo類樣式的div
需要注意的是返回的nodeList集合中的元素是非實(shí)時的.

相關(guān)文章

最新評論