如何从 CSS 访问样式

发布于 2025-01-02 08:22:25 字数 287 浏览 2 评论 0原文

如果使用CSS资源,我们可以从代码中的CSS定义中获取值吗?

例如,

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

我们可以通过代码知道 width 的值并且我想从我的 java 类之一进行访问吗?

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆之渊 2025-01-09 08:22:25
var width = $('.iv_menu_thumbnail').width();
console.log(width);

如果这是您所要求的,这将获得元素的宽度。
就我而言,你无法从 css 声明中获取非数值。

但是你可以使用 jQuery 通过 jQuery 设置你自己的值,

.css()

所以如果你想设置一个新的 css 值,它看起来像这样。 (或覆盖它)

$(someelement).css('float', 'left');
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');
我喜欢麦丽素 2025-01-09 08:22:25

据我所知,您只能检查应用了该属性的元素上的计算 CSS 属性。喜欢:

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

$(someElementOrId).width();

请注意,前者和后者不同 - 前者不包含计量单位,后者包含。

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.

暖阳 2025-01-09 08:22:25

您可以在 Css 资源文件中包含一个变量,并使用该变量设置宽度属性,然后从代码中访问宽度变量。

CssResource

Css 资源文件

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

Java 代码

interface MyResources extends CssResource {
  int small();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文