带有多个/多个伪选择器/匹配项的原生 Javascript querySelectorAll() [英] Native Javascript querySelectorAll() with multiple/many pseudo selectors/matches
问题描述
如何在原生 Javascript querySelectorAll() 中放置多个伪选择器?
How can I place many pseudo selectors inside the native Javascript querySelectorAll()?
示例:我想搜索一个 id 以 [id^=starting] 开头并以 [id$=ending] 结尾的元素.(找不到现有问题,所以自己制作并回答)
Example: I want to search for an element with an id that starts with [id^=starting] and ends with [id$=ending]. (Couldn't find existing question so making my own and answering it)
推荐答案
使用 Native Javascript 这将是代码:
With Native Javascript this would be the code:
document.querySelectorAll('[id^=starting][id$=ending]');
或
document.querySelectorAll('[id^='+startingString+'][id$='+endingString+']');
这将得到一个以指定字符串开头并以指定字符串结尾的元素.
This will get an element which starts with the specified string AND ends with the specified string.
并做一个或",在它们之间放一个空格:
And to do an "OR", put a space between them:
document.querySelectorAll('[id^=starting] [id$=ending]');
这篇关于带有多个/多个伪选择器/匹配项的原生 Javascript querySelectorAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!