开发者

Detect if element has padding using jQuery

开发者 https://www.devze.com 2023-04-06 07:30 出处:网络
How could I d开发者_C百科etect if an element has padding applied to it? So something like: if($(\'div.MiddleColumn\') has padding of 5px) {

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
    */
}
0

精彩评论

暂无评论...
验证码 换一张
取 消