开发者

How to select multiple div by his unique id number

开发者 https://www.devze.com 2022-12-21 16:39 出处:网络
is there a way to select multiple div with css?? like div id=\"text-box4\" div id=\"text-box5\" div id=\"text-box7\"

is there a way to select multiple div with css??

like

div id="text-box4"
div id="text-box5"
div id="text-box7"

开发者_StackOverflow社区etc


Native to ie7,ie8 and any other browser that accepts "substring matching attribute selectors" (cf. http://www.w3.org/TR/css3-selectors/), you can use the following syntax to select elements with multiple similar ids:

div[id^='text-box']

This basically says to the parsing engine, "select all div elements that have an id attribute which begins with 'text-box'

QRC:

[attribute^='text'] = attributes that STARTS with 'text'

[attribute$='text'] = attributes that END with 'text'

[attribute*='text'] = attributes that CONTAINS 'text'


CSS doesn't have a wildcard for that.

However if you use jQuery you can:
http://api.jquery.com/attribute-contains-selector/ or
http://api.jquery.com/attribute-contains-word-selector/

<div id="text-box4"></div>
<div id="text-box5"></div>
<div id="text-box7"></div>
<script>$("div[id*='text-box']").css("color", "red");</script>


like this?

#text-box4, 
#text-box5, 
#text-box7 {
    /* your properties here */
}


CSS classes are designed for selecting multiple elements:

<div id="text-box4" class="my-text-box"/>
<div id="text-box5" class="my-text-box"/>
<div id="text-box7" class="my-text-box"/>


maerics' answer is correct. The CSS selector used to select the divs in that case would be:

.my-text-box {
  /* Styles go here */
}
0

精彩评论

暂无评论...
验证码 换一张
取 消