jQuery css('padding')-Firefox问题 [英] jQuery css('padding') - Issue with Firefox
问题描述
Firefox(至少v19.0.2以上)似乎与jQuery css()函数有关,当您尝试获取元素的padding
.
It seems Firefox (at least v19.0.2) has an issue with jQuery css() function, when you try to get an element's padding
.
使用.css('padding-left')
时我没有问题,但是最好写一行而不是4行,尤其是在像这样的简单功能上.
I don't have the problem when I use .css('padding-left')
, but it would be nice to write a single line instead of 4, particularly on a simple feature like this one.
以下是显示我的问题的示例,请比较Chrome/Firefox的行为:
Here's a sample to show my problem, please compare Chrome/Firefox behaviors:
$('#log').append(
'padding : '+ $('#sample').css('padding')+'\n'+
'padding-top : '+ $('#sample').css('padding-top')+'\n'+
'padding-bottom : '+ $('#sample').css('padding-bottom')+'\n'+
'padding-left : '+ $('#sample').css('padding-left')+'\n'+
'padding-right : '+ $('#sample').css('padding-right')+'\n'
);
#sample {
border: 1px solid black;
padding: 8px;
margin: 10px;
}
#log {
padding: 8px;
margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<div id="sample">Lorem ipsum</div>
<textarea id="log" rows="10" cols="50"></textarea>
我错过了什么吗?还是有一种解决方法可以在不调用4个函数的情况下检索元素的padding
?
Am I missing anything ? Or is there a workaround to retrieve an element's padding
without calling 4 functions ?
谢谢.
推荐答案
请参见错误#13421 (尽管这实际上不是错误).
See bug #13421 (although this is not actually a bug).
css()的文档说:
速记CSS属性(例如
margin
,background
,border
)不是 支持的.例如,如果要检索渲染的边距, 使用:$(elem).css('marginTop')
和$(elem).css('marginRight')
,依此类推 上.
Shorthand CSS properties (e.g.
margin
,background
,border
) are not supported. For example, if you want to retrieve the rendered margin, use:$(elem).css('marginTop')
and$(elem).css('marginRight')
, and so on.
padding
是这样的属性,因此您确实必须使用paddingLeft
,paddingRight
等.
padding
is such a property, so you will indeed have to use paddingLeft
, paddingRight
, etc.
这篇关于jQuery css('padding')-Firefox问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!