简体   繁体   English

可以使用jquery定位类吗?

[英]Can you target classes with jquery?

What I want to do is $('.class.img').css('cellpadding', variable); 我想做的是$('.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. CSS像style="..."一样内联应用样式属性,并且不修改.class本身。

$('.class').css does not do anything. $('.class').css不执行任何操作。

$('.class').css('color','red') writes a color style $('.class').css('color','red')编写颜色样式

$('.class').css('color') reads the color style $('.class').css('color')读取颜色样式

So to target your img elements within an element with class "cellBox": 因此,将您的img元素定位在具有“ cellBox”类的元素中:

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

(That sets border , but you can set padding the same way.) (设置border ,但是您可以用相同的方式设置padding 。)

Check working example at http://jsfiddle.net/SBjfp/2/ 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.) (请注意,jQuery文档说不支持诸如paddingborder速记属性;这主要适用于获取属性; [如上所述]的设置通常可行,因为底层浏览器的实现支持该设置。)

The jQuery selector engine works perfectly well with class selectors. jQuery选择器引擎与类选择器完美配合。

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') ). 唯一明显与此相关的问题(可能还有其他问题,但是您未提供任何上下文(例如文档的外观或运行语句的内容))是css是一个函数,而您没有调用它(即css('foo') )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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