document.querySelector

本文介绍querySelector方法,该方法采用文档节点的深度优先预序遍历,返回文档中与指定选择器组匹配的第一个元素。若未找到匹配项则返回null,若选择器语法错误则抛出SYNTAX_ERR异常。

Summary

Returns the first element within the document (using depth-first pre-order traversal of the document's nodes) that matches the specified group of selectors.

Syntax

element
 = document.querySelector(selectors
);

where

  • element   is an  element   object.
  • selectors   is a string containing one or more CSS selectors separated by commas.

Example

In this example, the first element in the document with the ID "myid " is returned:

1 var   el = document.querySelector( "#myid" );

Notes

Returns  null   if no matches are found; otherwise, it returns the first matching element.

Throws a  SYNTAX_ERR   exception if the specified group of selectors is invalid.

querySelector()   was introduced in the Selectors API.

Browser compatibility

  • Desktop
  •  
  • Mobile
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support13.5 (1.9.1)
bug 416317
8103.2 (525.3)
WebKit bug 16587

Specification

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值