最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

string matching - Native Javascript querySelectorAll() with multiplemany pseudo selectorsmatches - Stack Overflow

programmeradmin3浏览0评论

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)

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)

Share Improve this question asked May 24, 2013 at 6:06 goamngoamn 2,1462 gold badges27 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

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]');

If you need to query for the elements starting with a string or with another string, you have to use the following structure:

document.querySelectorAll('[id ^= "startingString-"], [id ^= "anotherStartingString-"]')
发布评论

评论列表(0)

  1. 暂无评论