国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

各瀏覽器中querySelector和querySelectorAll的實現差異分析

2024-05-06 14:21:57
字體:
來源:轉載
供稿:網友

querySelector和querySelectorAll是W3C提供的新的查詢接口

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

1、querySelector只返回匹配的第一個元素,如果沒有匹配項,返回null。
2、querySelectorAll返回匹配的元素集合,如果沒有匹配項,返回空的nodelist(節點數組)。
3、返回的結果是靜態的,之后對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(IE8中的selector string只支持css2.1的)
瀏覽器支持查看

module dom {
[Supplemental, NoInterfaceObject]
interface NodeSelector {
Element querySelector(in DOMString selectors);
NodeList querySelectorAll(in DOMString selectors);
};
Document implements NodeSelector;
DocumentFragment implements NodeSelector;
Element implements NodeSelector;
};

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 3.5 (1.9.1)
bug 416317
9
8 (CSS2 selectors only)
10 3.2 (525.3)
WebKit bug 16587
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 yes 9 10.0 3.2

從接口定義可以看到Document、DocumentFragment、Element都實現了NodeSelector接口。即這三種類型的元素都擁有者兩個方法。querySelector和querySelectorAll的參數須是符合 css selector 的字符串。不同的是querySelector返回的是一個對象,querySelectorAll返回的一個集合(NodeList)。

目前 IE8/9及Firefox/Chrome/Safari/Opera 的最新版已經支持它們。

如想獲取頁面class屬性為"red"的元素,除了使用document.getElementsByClassName('red')還可以使用document.querySelector('.red')和document.querySelectorAll('.red')。

但Element.querySelector和Element.querySelectorAll的實現有錯誤,如下

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 剑河县| 阳谷县| 玉林市| 永福县| 孟连| 海林市| 和龙市| 台湾省| 兴仁县| 武清区| 休宁县| 江阴市| 永和县| 彰武县| 绥江县| 普陀区| 康马县| 开平市| 巴马| 大余县| 加查县| 仪征市| 瑞安市| 隆昌县| 玛多县| 浮山县| 赞皇县| 永和县| 木里| 日喀则市| 西乌珠穆沁旗| 玉门市| 望江县| 麻栗坡县| 修文县| 巴南区| 视频| 万州区| 岚皋县| 循化| 佛坪县|