document.querySelector和querySelectorAll方法

querySelector和querySelectorAll是W3C提供的 新的查詢接口,其主要特色以下:css

一、querySelector只返回匹配的第一個元素,若是沒有匹配項,返回null。 
二、querySelectorAll返回匹配的元素集合,若是沒有匹配項,返回空的nodelist(節點數組)。 
三、返回的結果是靜態的,以後對document結構的改變不會影響到以前取到的結果。 
 
這兩個方法均可以接受三種類型的參數:id(#),class(.),標籤,很像jquery的選擇器。
 
var obj = document.querySelector("#id");
var obj = document.querySelector(".classname");
var obj = document.querySelector("div");
var el = document.body.querySelector("style[type='text/css'], style:not([type])");
var elements = document.querySelectorAll("#score>tbody>tr>td:nth-of-type(2)");
var elements = document.querySelectorAll("#id1, #id2, .class1, class2, div a, #list li img");目前IE8+,ff,chrome都支持此api
相關文章
相關標籤/搜索