how to access the styles from CSS

一曲冷凌霜 提交于 2019-12-13 05:48:30

问题


Can we get the value from inside a css definition in the code if CSS resource is used?

e.g.

.iv_menu_thumbnail {  
  display: block;  
  width: 24px;  
  height: 24px;  
  margin: 3px;  
  float: left;  
  cursor: pointer;  
}`

Can we know via code the value of width and i want to access from one of my java class?

Thanks in advance


回答1:


var width = $('.iv_menu_thumbnail').width();
console.log(width);

This will get the width of the element if this is what youre asking for. As far as I'm concerned you cannot get non numerical values from a css declaration.

But you can set your own values via jQuery using the

.css()

So it would look like this if you want to set a new css value. (or overwrite it)

$(someelement).css('float', 'left');



回答2:


As far as I know, you can only inspect the computed CSS property on an element where this has been applied. Like:

$(someElementOrId).css('width');

or

$(someElementOrId).width();

Note that the former and the latter differ - the former does not contain the unit of measure, the latter does.




回答3:


You can have a variables in your Css Resource file and set the width attribute with that variable, and then access the width varialbe from code.

CssResource

Css Resource file

@def small 1px; 
@def black #000;
border: small solid black;

Java Code

interface MyResources extends CssResource {
  int small();
}


来源:https://stackoverflow.com/questions/9128023/how-to-access-the-styles-from-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!