HTML DOM Document querySelector() harshe
- Shang yi ye open()
- Xia yi ye querySelectorAll()
- Fanhui cheng yiceng HTML DOM Documents
Definition and usage
querySelector()
The method returns elements that match the CSS selectorFirstelements.
To returnAllmatches (not just the first match), please use querySelectorAll().
If the selector is invalid, then querySelector()
He querySelectorAll()
will throw SYNTAX_ERR
Exception.
Example
Example 1
Get the first <p> element:
document.querySelector("p");
Example 2
Get the first element with class="example":
document.querySelector(".example");
Example 3
Get the first <p> element with class="example":
document.querySelector("p.example");
Example 4
Change the text of the element with id="demo":
document.querySelector("#demo").innerHTML = "Hello World!";
Example 5
Select the first <p> element whose parent element is the <div> element:
document.querySelector("div > p");
Example 6
Select the first <a> element with the "target" attribute:
document.querySelector("a[target]");
Example 7
Select the first <h3> or the first <h4>:
<h3>A h3 element</h3> <h4>A h4 element</h4> document.querySelector("h3, h4").style.backgroundColor = "red";
Example 8
Select the first <h3> or the first <h4>:
<h4>A h4 element</h4> <h3>A h3 element</h3> document.querySelector("h3, h4").style.backgroundColor = "red";
Syntax
document.querySelector(cssSelectors)
Parameter
Parameter | Description |
---|---|
cssSelectors |
Mandatory. A ko manyan CSS selector. CSS selector a raiwa da HTML element a raiwa id, class, type, attribute, value da sauransu. Don tsarin yanki na kaiyawa, ka ci gaba da CSS Selectors Kita. Da a raiwa tsa manyan selector, ka tsama kuma tsa gargareshi (ka tsama da shaidar da yana shida). |
Return value
Type | Description |
---|---|
Object |
Nodelist da a raiwa tsa babban element da a raiwa da CSS selector. Idan anoma na a raiwa, a raiwa akaiwa null. |
Firawsun HTMLCollection da NodeList tsa
NodeList He HTMLcollection Feichang xiangsi。
Shi liang ge dou shi cong wendan zhong qu chu de jie dian (yuan su)de xiangli bu du de jihui (liebiao)。Kai guan jie dian neng tongguo suoding hao ma (xiaobiao)kan chi jie dian。Ji suoding hao ma zhi yu 0 kaishi。
Shi liang ge dou you length ShuXing,ta fanhui liebiao (jihui)zhong yuan su de shu liang。
HTMLCollection shiWendan yuan suDe jihui.
NodeList shiWendan jie dian(yuan su jie dian, shuXing jie dian he wenben jie dian)de jihui。
HTMLCollection xiangmu kan chi guo ta men de mingcheng, id huo suoding hao ma.
NodeList xiangmu kan chi guo ta men de suoding hao ma.
HTMLCollection zhi yong shiShi shiDe jihui.
Ri chi:Ri guo jia ru <li> yuan su ji dianzai DOM zhong de liebiao, zhe HTMLCollection zhong de liebiao yeqie hui gai bian。
NodeList shi chongxuJingtiDe jihui.
Ri chi:Ri guo jia ru <li> yuan su ji dianzai DOM zhong de liebiao, zhe NodeList zhong de liebiao bu hui gai bian。
getElementsByClassName()
He getElementsByTagName()
Fangfa dou fanhui shi shi HTMLCollection.
querySelectorAll()
Fangfa fanhui jingti NodeList.
childNodes
ShuXing fanhui shi shi shi NodeList.
Chulai Zhichi
document.querySelector()
Shi DOM Level 1 (1998) TeXing.
Suoyou hua she dui zhi chi chi zhe ta:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
ZhiChi | 9-11 | ZhiChi | ZhiChi | ZhiChi | ZhiChi |
Xiangguan YeMian
Jiaocheng:
QuerySelector Fangfa:
GetElement Fangfa:
- Shang yi ye open()
- Xia yi ye querySelectorAll()
- Fanhui cheng yiceng HTML DOM Documents