This question already has answers here:
How to include !important in jquery
                                
                                    (8个答案)
                                
                        
                                2年前关闭。
            
                    
我正在尝试使用以下语句应用内联CSS。

$('.WarningCount').parent().css('margin-left', '-1.4% !important;');


但这是行不通的,它不会仅被应用。我在开发人员控制台中看不到此样式。我必须重写整个样式才能使其正常工作。

$('.WarningCount').parent().attr('style', 'margin-left:-1.4% !important;');


任何原因都可能是这里的真正问题。

最佳答案

取自jQuery documentation


  注意:.css()忽略!important声明。因此,语句$(“ p”
  ).css(“ color”,“ red!important”)不会改变所有颜色
  页面中的段落变为红色。强烈建议使用类
  代替;否则使用jQuery插件。


不过,您可以参考this answer并使用cssText属性

const target = $('.WarningCount').parent();
target.css('cssText', target.attr('style')+'margin-left: -1.4% !important;');

关于jquery - jQuery .css不适用于父容器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52002012/

10-14 14:52
查看更多