Using !important in jQuery's css() function

前端 未结 7 1867
长情又很酷
长情又很酷 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: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

     
    YOUR STUFF

    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('')
    $('#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

提交回复
热议问题