How to apply !important using .css()?

后端 未结 30 2984
情深已故
情深已故 2020-11-21 07:06

I am having trouble applying a style that is !important. I’ve tried:

$(\"#elem\").css(\"width\", \"100px          


        
30条回答
  •  一向
    一向 (楼主)
    2020-11-21 07:36

    This solution doesn't override any of the previous styles, it just applies the one you need:

    var heightStyle = "height: 500px !important";
    if ($("foo").attr('style')) {
      $("foo").attr('style', heightStyle + $("foo").attr('style').replace(/^height: [-,!,0-9,a-z, A-Z, ]*;/,''));
    else {
      $("foo").attr('style', heightStyle);
    }
    

提交回复
热议问题