How to apply !important using .css()?

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

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

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


        
30条回答
  •  萌比男神i
    2020-11-21 07:46

    This solution will leave all the computed javascript and add the important tag into the element: You can do (Ex if you need to set the width with the important tag)

    $('exampleDiv').css('width', '');
    //This will remove the width of the item
    var styles = $('exampleDiv').attr('style');
    //This will contain all styles in your item
    //ex: height:auto; display:block;
    styles += 'width: 200px !important;'
    //This will add the width to the previous styles
    //ex: height:auto; display:block; width: 200px !important;
    $('exampleDiv').attr('style', styles);
    //This will add all previous styles to your item
    

提交回复
热议问题