简体   繁体   中英

Can you target classes with jquery?

What I want to do is $('.class.img').css('cellpadding', variable);

It doesn't seem to be working. I tried googling to no avail. Any help would be appreciated. I'm trying to apply it to an image inside the element with the given class.

CSS applies the styling properties inline as in style="..." and does not modify the .class itself.

$('.class').css does not do anything.

$('.class').css('color','red') writes a color style

$('.class').css('color') reads the color style

So to target your img elements within an element with class "cellBox":

var borderx = "1px solid red";
$('.cellbox img').css('border',borderx);

(That sets border , but you can set padding the same way.)

Check working example at http://jsfiddle.net/SBjfp/2/

(Note that the jQuery documentation says that shorthand properties like padding or border are not supported ; this mostly applies to getting the properties; setting [as above] usually works because it's supported by the underlying browser's implementation.)

The jQuery selector engine works perfectly well with class selectors.

The only thing clearly wrong with this (there might be other things, but you haven't provided any context (such as what the document looks like or when the statement is run)) is that css is a function, and you aren't calling it (ie css('foo') ).

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