jQuery CSS方法

时间:2011-08-25 11:53:22

标签: jquery css

HTML:

<div id="that" style="margin-left: 10px; border-width: 2px"></div>

jQuery的:

// alerts "10px 0px" which is strange, isn't it?
alert($('#that').css('margin-left') + ' ' +  $('#that').css('border-width'));

jsFiddle:http://jsfiddle.net/vZpAv/

为什么jQuery的行为如此?是不是应该得到2px?首先,我认为这是因为CSS属性中的破折号(因为css('borderWidth')正常工作)但是你可以看到它对margin-left的效果很好。

3 个答案:

答案 0 :(得分:7)

border-width用于设置元素上每个边框的边框宽度,但不能假设每个边框具有相同的宽度。因此,您需要运行以下内容:

$('#that').css("borderTopWidth");

但是这仍然会返回0px,因为如果不设置颜色和样式,就无法设置边框宽度。

答案 1 :(得分:1)

使用Fire fox和fire bug并尝试使用

Console.log();代替警报

答案 2 :(得分:0)

您可以使用以下代码:

alert($('div').css('margin-left') + ' ' + $.style($('div')[0], 'borderWidth'));
相关问题