Native Javascript querySelectorAll() with multiple/many pseudo selectors/matches

為{幸葍}努か 提交于 2019-12-24 06:42:21

问题


How can I place many pseudo selectors inside the native Javascript querySelectorAll()?

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)


回答1:


With Native Javascript this would be the code:

document.querySelectorAll('[id^=starting][id$=ending]');

or

document.querySelectorAll('[id^='+startingString+'][id$='+endingString+']');

This will get an element which starts with the specified string AND ends with the specified string.

Edit: And to do an "OR", put a space between them:

document.querySelectorAll('[id^=starting] [id$=ending]');



来源:https://stackoverflow.com/questions/16728600/native-javascript-queryselectorall-with-multiple-many-pseudo-selectors-matches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!