How to apply !important using .css()?

后端 未结 30 3021
情深已故
情深已故 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:21
    const elem = $("#elem");
    elem[0].style.removeAttribute('width');
    elem[0].style.setProperty('width', '100px', 'important');
    

    Note: Using Chrome may return an error such as:

    elem[0].style.removeAttribute is not a function

    Changing the line to use the .removeProperty function such as to elem[0].style.removeProperty('width'); fixed the issue.

    0 讨论(0)
  • 2020-11-21 07:21

    Here is what I did after encountering this problem...

    var origStyleContent = jQuery('#logo-example').attr('style');
    jQuery('#logo-example').attr('style', origStyleContent + ';width:150px !important');
    
    0 讨论(0)
  • 2020-11-21 07:21

    https://jsfiddle.net/xk6Ut/256/

    An alternative approach is dynamically creating and updating CSS class in JavaScript. To do that, we can use style element and need to employ the ID for the style element so that we can update the CSS class

    function writeStyles(styleName, cssText) {
        var styleElement = document.getElementById(styleName);
        if (styleElement) document.getElementsByTagName('head')[0].removeChild(
            styleElement);
        styleElement = document.createElement('style');
        styleElement.type = 'text/css';
        styleElement.id = styleName;
        styleElement.innerHTML = cssText;
        document.getElementsByTagName('head')[0].appendChild(styleElement);
    }
    

    ...

      var cssText = '.testDIV{ height:' + height + 'px !important; }';
      writeStyles('styles_js', cssText)
    
    0 讨论(0)
  • 2020-11-21 07:22

    Three working examples

    I had a similar situation, but I used .find() after struggling with .closest() for a long time with many variations.

    The Example Code

    // Allows contain functions to work, ignores case sensitivity
    
    jQuery.expr[':'].contains = function(obj, index, meta, stack) {
        result = false;
        theList = meta[3].split("','");
        var contents = (obj.textContent || obj.innerText || jQuery(obj).text() || '')
        for (x=0; x<theList.length; x++) {
            if (contents.toLowerCase().indexOf(theList[x].toLowerCase()) >= 0) {
                return true;
            }
        }
        return false;
    };
    
    $(document).ready(function() {
        var refreshId = setInterval( function() {
            $("#out:contains('foo', 'test456')").find(".inner").css('width', '50px', 'important');
        }, 1000); // Rescans every 1000 ms
    });
    

    Alternative

    $('.inner').each(function () {
        this.style.setProperty('height', '50px', 'important');
    });
    
    $('#out').find('.inner').css({ 'height': '50px'});
    

    Working: http://jsfiddle.net/fx4mbp6c/

    0 讨论(0)
  • 2020-11-21 07:25

    I think it works OK and can overwrite any other CSS before (this: DOM element):

    this.setAttribute('style', 'padding:2px !important');
    
    0 讨论(0)
  • 2020-11-21 07:26

    David Thomas’s answer describes a way to use $('#elem').attr('style', …), but warns that using it will delete previously-set styles in the style attribute. Here is a way of using attr() without that problem:

    var $elem = $('#elem');
    $elem.attr('style', $elem.attr('style') + '; ' + 'width: 100px !important');
    

    As a function:

    function addStyleAttribute($element, styleAttribute) {
        $element.attr('style', $element.attr('style') + '; ' + styleAttribute);
    }
    
    addStyleAttribute($('#elem'), 'width: 100px !important');
    

    Here is a JS Bin demo.

    0 讨论(0)
提交回复
热议问题