欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

jquery修改css样式,样式带!important 好像不太对

shiping1 的头像
由于需求的需要,今天在用jquery修改一个弹出框的样式的时候,由于有一个按钮有padding-left:12px;导致内间距空出来的这一块颜色用普通的方式无法改变。

普通的jquery修改css的方法:$("#idName").css("backgroundColor","red");但是这个方法无法改变padding-left:12px;这一块的背景色,因为定位不到它的id。

后来在请教了公司的搞html的同事(民庆)后,他说要在样式后面加个!important,然后我在google开发人员工具上试了一下,果然可以了。但是直接写$("#idName").css("backgroundColor","red!important");不能实现,而且让red样式都不可用了。

后来在网上又找到了一个方法:$("#idName").css("cssText","background-color:red!important");这样就能够解决了。( 注意:后面是background-color:red!important,而不是backgroundColor:red!important,我在这里卡了半天,找了好久才找到这个原因,这个让我一开始用这个方法时的css没有效果。 )

来自 http://www.tuicool.com/articles/Z777F3q

display: block!important;//页面上是这样写的 
//修改的话,这样做 
$("#selCont").css("cssText","display: none!important"); 

来自 
http://www.jb51.net/css/191659.html

 

jQuery and JavaScript CSS !important

Everyone who is familiar with CSS !important statement understood that it means that the style have the highest priority regardless of other priority. However, !important should be avoided as much as possible and order priority should be practice in CSS instead. In jQuery, there are times when we need to change the styling of our web program but programmers usually lack of such knowledge and inquire whether !important is available for jQuery css() method. In this article, we will discuss jQuery css() and JavaScript methods in term of using !important definition.

jQuery CSS !important

One thing that everyone must have wonder (those that haven't tried yet) is whether jQuery support declaration of !important for their css() method such as this.

1
jQuery('#id').css('height', '200px !important');

You will find that nothing will happen with a declaration of such manipulation. If we were to try out on pure JavaScript with the above declaration, it will be something like this.

1
document.getElementById('id').style.height = "200px !important";

Having a pure JavaScript instruction such as this will not work in most browser other than safari 3++. Hence, jQuery will definitely won't work too. This is not a bug but something that most browser doesn't acknowledge a need since inline style already have the highest priority other than user defined ones. (unless you do not want your user to change how they view your website).

Solution to jQuery CSS !important

Well, the typical way of getting !important will not work. Rather, you might want to try CSS DOM cssText. Hence, you will force !important to your jQuery statement by doing this.

1
jQuery('#id').css('cssText', 'height: 200px !important');

However, most people will be unfamiliar with cssText. cssText will overwrites the css of that particular element. Hence, you will have to redeclare the rule for that particular element all over again. On the other hand, if you are using JavaScript cssText, different browser treat cssText different such as ie you will define this way,

1
document.getElementById('id').style.cssText = 'height: 200px !important'

While normally you will use it this way.

1
document.getElementById('id').cssText = 'height: 200px !important'

Definitely, there is a better alternative such as using the cssRules/rules array which can specify the rule in that element to be modify.

The other more practical way of doing it is by introducing new class for your element. Instead of styling through jQuery or DOM object which is applying inline styling (means highest priority already), you should leave every majority styling or best all of the styling to an external style sheet that help you reduce some bytes on the script and also improve maintainability for both your code and style. You can do this using the toggleClass which adds a new class to that element automatically.

1
jQuery('#id').toggleClass('classname');

or you can do this in JavaScript by replacing the class (this is more efficient than the jQuery one since there isn't any additional checking and stuff)

1
document.getElementById('id').className = 'classname';

Summary

Using !important in CSS is not advisable since it might kill your web usability. Furthermore, there is no reason why !important should be use as inline styling already has the highest priority. Hence, you might want to reconsider applying !important on your script after thinking about the consequences that might bought to your users.

来自 http://hungred.com/how-to/jquery-javascript-css-important/


普通分类: