Javascript 如何使用 JQuery 获取 CSS 属性的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5072951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the value of a CSS property using JQuery?
提问by Sam
I am trying to set a box A to jump to the same x-position as box B. How do I do this? I tried the following but it does not work (the second line):
我正在尝试将框 A 设置为跳到与框 B 相同的 x 位置。我该怎么做?我尝试了以下但它不起作用(第二行):
$("#boxa").animate({
left: $("#boxb").left
}, 1000, function() { });
});
Thanks,
谢谢,
Sam
山姆
回答by Andy
either
任何一个
$('#boxB').css('left');
or
或者
$('#boxB').offset().left;
回答by Yster
For properties specified in pixels, using:
对于以像素为单位指定的属性,使用:
$('#boxB').css('padding-left');
gives a string like: '22px'
给出一个字符串,如:'22px'
Maybe it's better to use:
也许最好使用:
parseInt($('#boxB').css('padding-left'), 10);
in such cases, which gives a number like 22 .
在这种情况下,它会给出类似 22 的数字。