admin管理员组

文章数量:1418092

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.

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.

Share Improve this question edited Mar 20, 2011 at 0:07 T.J. Crowder 1.1m200 gold badges2k silver badges2k bronze badges asked Mar 19, 2011 at 8:09 GazowGazow 1,0792 gold badges11 silver badges16 bronze badges 7
  • 1 What do you want to do? Do you want the CSS definition for that class ? Do you want to apply some CSS to elements with that class? – alexn Commented Mar 19, 2011 at 8:15
  • $('.cellBox:img').css("padding", cellPadding); specifically im trying to do this, where cellPadding is a variable, im trying to apply it to an image inside a div with class'cellBox' – Gazow Commented Mar 19, 2011 at 8:17
  • @Gazow: The correct CSS syntax for an img element with the "cellBox" class is img.cellBox. So: $("img.cellBox").css("padding", cellPadding); But note that css does not support CSS shorthand properties like padding. – T.J. Crowder Commented Mar 19, 2011 at 8:21
  • @TJ, im trying to apply it to images that are children of the .cellBox Class, sorry i messed this question up pretty bad, i was googling like 10 different things trying to get this working heh. the images are created dynamically with a for loop so they do not have class or id – Gazow Commented Mar 19, 2011 at 8:22
  • ok, yeah i guess i messed this whole thing up, i think its fixed now thanks – Gazow Commented Mar 19, 2011 at 8:26
 |  Show 2 more ments

2 Answers 2

Reset to default 3

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/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 (i.e. css('foo')).

本文标签: javascriptCan you target classes with jqueryStack Overflow