selectors-api

Can't use querySelectorAll for a single element? [duplicate]

喜夏-厌秋 提交于 2021-02-05 08:46:13
问题 This question already has answers here : What do querySelectorAll and getElementsBy* methods return? (10 answers) Closed 12 days ago . I understand that under normal circumstances, you would use querySelector to select a single element and querySelectorAll for multiple. However, I was surprised to discover that querySelectorAll doesn't work with a single element. I expected it to work with one OR more. I can't find anything that says it shouldn't work with just one so I'm asking here if that

Difference between querySelector() and querySelectorAll()[0]

空扰寡人 提交于 2020-11-29 08:41:11
问题 I ran across some JS code using the following to select the first of multiple nodes. querySelectorAll()[0] Isn't the following doing the exact same thing? querySelector() Is there an advantage of using querySelectorAll()[0] ? 回答1: Both expressions will return the exact same result. The only difference is the querySelectorAll()[0] will first find all the items that match the selector and then index the first item. Whereas querySelector() will "short circuit" once it finds the first element. So

Difference between querySelector() and querySelectorAll()[0]

て烟熏妆下的殇ゞ 提交于 2020-11-29 08:38:11
问题 I ran across some JS code using the following to select the first of multiple nodes. querySelectorAll()[0] Isn't the following doing the exact same thing? querySelector() Is there an advantage of using querySelectorAll()[0] ? 回答1: Both expressions will return the exact same result. The only difference is the querySelectorAll()[0] will first find all the items that match the selector and then index the first item. Whereas querySelector() will "short circuit" once it finds the first element. So

Changing CSS pseudo-element styles via JavaScript [duplicate]

旧城冷巷雨未停 提交于 2020-05-09 17:27:33
问题 This question already has answers here : Selecting and manipulating CSS pseudo-elements such as ::before and ::after using jQuery (22 answers) How to update placeholder color using Javascript? (3 answers) Closed 2 months ago . Is it possible to change a CSS pseudo-element style via JavaScript? For example, I want to dynamically set the color of the scrollbar like so: document.querySelector("#editor::-webkit-scrollbar-thumb:vertical").style.background = localStorage.getItem("Color"); and I

Build the querySelector string value of any given node in the DOM

家住魔仙堡 提交于 2020-03-25 17:25:11
问题 I am trying to find a way to build the querySelector string of any given node. In other words - pick any node on a page - is it possible to walk up the DOM and build a string that would allow me to pass the generated string to document.querySelector and get back the node I chose? From what I can tell querySelector has a bug where you can only use nth-child once in the string. I have tried several times but so far have failed to find a solution. I want to do this in native JavaScript, not

Does querySelectorAll support the period(.) character in an id?

假装没事ソ 提交于 2020-02-03 04:18:06
问题 Does querySelectorAll support the period(.) character in an id? I mean if I append an element like below: var div = document.createElement('div'); div.id='my.divid'; document.body.appendChild(div); and then I use querySelectorAll like below: document.querySelectorAll('#my.divid'); then I get nothing! So period is legal character for id, and querySelectorAll is the official method which Firefox provided; I can't believe the method doesn't support period(.) in id. Did I make some mistake? 回答1:

Does querySelectorAll support the period(.) character in an id?

ⅰ亾dé卋堺 提交于 2020-02-03 04:17:17
问题 Does querySelectorAll support the period(.) character in an id? I mean if I append an element like below: var div = document.createElement('div'); div.id='my.divid'; document.body.appendChild(div); and then I use querySelectorAll like below: document.querySelectorAll('#my.divid'); then I get nothing! So period is legal character for id, and querySelectorAll is the official method which Firefox provided; I can't believe the method doesn't support period(.) in id. Did I make some mistake? 回答1:

Is there any way to get the text data of first non empty element using query selector

房东的猫 提交于 2020-01-25 21:43:02
问题 I have just started using querySelector . I want to get the text data of first non empty element without concerning the element name (p or div or span). For example: 1) <div> <span></span> <span>data</span> <span>data1</span> <div> 2) <div> <ul> <li></li> <li>data</li> </ul> <span>data1</span> <div> In both the cases I want to get the text data of first element that is non empty. In 1 case from the second span and in 2 case from the second li . Is that possible? 来源: https://stackoverflow.com

Compatibility of document.querySelectorAll in IE, Edge and Safari

十年热恋 提交于 2020-01-06 11:07:31
问题 Reference answer: https://stackoverflow.com/a/38399344/5132413 Please refer my question and the answer linked above works only in Firefox, Chrome, and Opera. I did some research and found that it (case insensitivity flag) isn't compatible. I need an equivalent in IE, Edge and Safari. var divs = document.querySelectorAll('div[class^="foo" i]'); 回答1: Since css level 4 is still in drafts, case-sensitivity selector is not compatible with most browsers. You may use filter method like this: var

Compatibility of document.querySelectorAll in IE, Edge and Safari

99封情书 提交于 2020-01-06 11:07:14
问题 Reference answer: https://stackoverflow.com/a/38399344/5132413 Please refer my question and the answer linked above works only in Firefox, Chrome, and Opera. I did some research and found that it (case insensitivity flag) isn't compatible. I need an equivalent in IE, Edge and Safari. var divs = document.querySelectorAll('div[class^="foo" i]'); 回答1: Since css level 4 is still in drafts, case-sensitivity selector is not compatible with most browsers. You may use filter method like this: var