简体   繁体   English

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

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

I'm trying to check if an inline style has already been applied by jQuery. 我正在尝试检查jQuery是否已经应用了内联样式。

Normally, I would do something like this: 通常,我会做这样的事情:

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

However, I don't know the percentage. 但是, 我不知道这个百分比。 What is the best way to check if there is any max-width applied? 检查是否应用了最大宽度的最佳方法是什么

By default .css('max-width') will return "none" if no max-width was defined, so the following condition should do the job: 默认情况下,如果没有定义max-width.css('max-width')将返回"none" ,因此以下条件应该完成工作:

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

If you need to check for inline style only , meaning that you override the original max-width CSS property (eg from the stylesheet file), you may do the following: 如果只需要检查内联样式 ,意味着覆盖原始的max-width CSS属性(例如,从样式表文件中),则可以执行以下操作:

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

... as pure maxWidth property of style will be empty "" if the CSS property was not set. ...如果未设置CSS属性,则stylemaxWidth属性将为空""

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

If on a element the max-width property is not applied then it will return none . 如果在元素上未应用max-width属性,则它将不返回none Try to check none 尝试检查none

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

OR 要么

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

Checking only the inline style : 仅检查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
}

OR 要么

I recommend to use Jquery selector Attribute Contains Word Selector 我建议使用Jquery选择器Attribute Contains Word Selector

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

Working Fiddle 工作小提琴

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

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