简体   繁体   中英

how i change css with same element id for multiple element

I have multiple div on same id, and i want change that all div backgorund.

function myfun(id) {
$('#pinsss').css({ background: "#0066ff"} );
}

this script working well to first element and others not working.

IDs should be unique and only one element on a page should have a given ID. Trying using a class instead.

note : keep in mind, id is unique. one element can have one id and it should be different from others. instead of id, if you can use class. because , class can use for any number of elements.

 #one{ width:100px; height:100px; margin:50px; border:4px solid orange; } #two{ width:100px; height:100px; margin:50px; border:8px solid red; } #three{ width:100px; height:100px; margin:50px; border:6px solid blue; } .all{ background-color:pink; } 
 <html> <head> <title></title> </head> <div class="all" id="one"></div> <div class="all" id="two"></div> <div class="all" id="three"></div> <body> </body> </html> 

like above code, each div has unique id .but same class , if you do any changes to the class , that will affect to all, which has same class, name. but id is different, styles apply only for the declared id .

First thing you should not use same id for all element, id should be unique to be ever element,

you should use class in this scenario, because you want to share the same behaviour for multiple element you can achieve this by using class,

Try to use different Id's for each element.

function myfun(firstId) {
$('#pinsss').css({ background: "#0066ff"} );
}

function myfun(secondId) {
$('#pinsss').css({ background: "#0066ff"} );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM