Using !important in jQuery's css() function

前端 未结 7 1865
长情又很酷
长情又很酷 2020-12-24 12:59

I have a dialog with an overlay declared like so:

     .ui-widget-overlay  {
         position: absolute;
         left: 8px;
         top: 9px;
         hei         


        
相关标签:
7条回答
  • 2020-12-24 13:08

    You can create a dynamic stylesheet with rules that override the properties you want and apply it on the page.

    var $stylesheet = $('<style type="text/css" media="screen" />');
    
    $stylesheet.html('.tall{height:1167px !important;} .short{height:985px !important}');
    
    $('body').append($stylesheet);
    

    Now, when you add our newly created classes, they will take precedence since they are the last defined.

    $('.ui-widget-overlay').addClass('tall');
    

    demo at http://jsfiddle.net/gaby/qvRSs/


    update

    For pre-IE9 support use

    var $stylesheet = $('<style type="text/css" media="screen">\
                        .tall{height:300px !important;}\
                        .short{height:100px !important}\
                        </style>');
    
    $('body').append($stylesheet);
    

    demo at http://jsfiddle.net/gaby/qvRSs/3/

    0 讨论(0)
  • 2020-12-24 13:14

    I solved this problem like this:

    inputObj.css('cssText', inputObj.attr('style')+'padding-left: ' + (width + 5) + 'px !IMPORTANT;');
    

    So no inline-Style is lost, an the last overrides the first

    0 讨论(0)
  • 2020-12-24 13:17

    You could try using $(this).attr('style', 'height:1167px !important'); I haven't tested it, but it should work.

    0 讨论(0)
  • 2020-12-24 13:20

    Unless I've misread your question, what you're doing does work in jsfiddle.

    EDIT: My fiddle only works in some browsers (so far, Chrome: pass, IE8: fail).

    0 讨论(0)
  • 2020-12-24 13:21

    Dont apply styles to a class. Apply a class to your div as a style!

    Let jQuery do all the work for you

    You style sheet should have these classes in them

    .ui-widget-overlay  {
             position: absolute;
             left: 8px;
             top: 9px;
             width: 518px !important; 
             }
    
    .ui-widget-small { height: 985px;  }
    
    .ui-widget-full { height: 1167px; }
    

    Ok thats your CSS sorted

    now your div

     <div id="myWidget" class="ui-widget-overlay ui-widget-small"> YOUR STUFF </div>
    

    Now you can use jQuery to manipulate your divs either by attaching to a button/click/hover whatever it is you wanna use

    $('#myWidget').removeClass('ui-widget-small').addClass('ui-widget-full')
    

    And you dont need to use !important - that is really used when you start having issues with large CSS files or several loaded styles.

    This is instant but you can also add an effect

    $('#myWidget').hide('slow', function(){ $('#myWidget').removeClass('ui-widget-small').addClass('ui-widget-full').show('slow') }  )
    

    You can add styles dynamically to your page like this- and to replace all existing classes with another class, we can use .attr('class', 'newClass') instead.

    $('body').prepend('<style type="text/css"> .myDynamicWidget { height: 450px; } </style>')
    $('#myWidget').attr('class', 'ui-widget-overlay')
    $('#myWidget').addClass('myDynamicWidget')
    

    But you do not want to be over writing your existing styles using this method. This should be used in a 'lost' case scenario. Just demonstrates the power of jQuery

    0 讨论(0)
  • 2020-12-24 13:21

    Please remove height attribute from class and then try to implement your if and else condition.

    .ui-widget-overlay  {
             position: absolute;
             left: 8px;
             top: 9px;
             width: 518px !important; 
             }
    
    if
         $('.ui-widget-overlay').attr("height", "985px");
    else
         $('.ui-widget-overlay').attr("height", "1167px");
    

    Enjoy code....keep smiling...

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