繁体   English   中英

在jQuery中检查是否设置了max-width?

[英]Check if max-width is set, in jQuery?

我正在尝试检查jQuery是否已经应用了内联样式。

通常,我会做这样的事情:

if ($(this).css('max-width') == '20%') {}

但是, 我不知道这个百分比。 检查是否应用了最大宽度的最佳方法是什么

默认情况下,如果没有定义max-width.css('max-width')将返回"none" ,因此以下条件应该完成工作:

if ($(this).css('max-width') !== 'none') { ... }

如果只需要检查内联样式 ,意味着覆盖原始的max-width CSS属性(例如,从样式表文件中),则可以执行以下操作:

if (this.style.maxWidth !== '') { ... }

...如果未设置CSS属性,则stylemaxWidth属性将为空""

if ( $(this).css('max-width')!='none' ) { }

如果在元素上未应用max-width属性,则它将不返回none 尝试检查none

$(this).css('max-width') =='none'

要么

if( $('#element').css('max-width') =='none' )  { 
 // max-width not EXISTS
} 
else
{
 // max-width EXISTS
}

仅检查inline style

if($('#element').attr('style') != 'undefined'){
 if( $('#element').attr('style').indexOf('max-width') == -1 )  { 
  // max-width not EXISTS
 } 
 else
 {
   // max-width EXISTS
 }
}
else
{
  // Inline style not EXISTS
}

要么

我建议使用Jquery选择器Attribute Contains Word Selector

$('div[style ~= "min-width:"]')

工作小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM