Detect if element has padding using jQuery
How could I d开发者_C百科etect if an element has padding applied to it?
So something like:
if($('div.MiddleColumn') has padding of 5px) {
do something
}
else
{
do something else
}
This will return the total horizontal padding of your element :
var hz_padding = $("el").innerWidth() - $("el").width();
.innerWidth()
returns the same value as .width()
, plus the padding. So hz_padding
will be > 0 if $("el")
has any padding.
The interesting thing here is that you will have the computed styles, which might be different from those in the stylesheet.
More on .innerWidth() and .width().
Use .outerWidth()
if you want to include borders, and .outerWidth(true)
to include margin.
And there are obviousy the same methods for the height.
var hasPadding = false,
element = $('div.MiddleColumn');
$.each(['top', 'right', 'bottom', 'left'], function(i, side) {
if (parseInt(element.css('padding-' + side))) {
hasPadding = true;
return false;
}
}
I would try the following:
if($('div.MiddleColumn').css('padding') == "5px") {
Use the .css
method
if ( $('div.MiddleColumn').css('padding-left') === '5px' ) {
do something
}
else {
do something else
}
Try this :
if($('div.MiddleColumn').css("padding") == "5px")
{
/*
do something
*/
}
else
{
/*
do something else
*/
}
精彩评论