Adding and removing style attribute from div with jquery

前端 未结 7 2004
走了就别回头了
走了就别回头了 2021-01-30 00:53

I\'ve inherited a project I\'m working on and I\'m updating some jquery animations (very little practice with jquery).

I have a div I need to add and remove the style at

相关标签:
7条回答
  • 2021-01-30 01:07

    Anwer is here How to dynamically add a style for text-align using jQuery

    0 讨论(0)
  • 2021-01-30 01:08

    The easy way to handle this (and best HTML solution to boot) is to set up classes that have the styles you want to use. Then it's a simple matter of using addClass() and removeClass(), or even toggleClass().

    $('#voltaic_holder').addClass('shiny').removeClass('dull');
    

    or even

    $('#voltaic_holder').toggleClass('shiny dull');
    
    0 讨论(0)
  • 2021-01-30 01:09

    Remove style attribute from div using J query:

    $("#TableDiv").removeAttr("style");
    

    Add style to div using J query:

    $("#TableDiv").attr("style", "display: none;");
    

    Add style using html:

    <div class="row" id="TableDiv" style="display: none;">
    </div>
    

    Hope it will helpful :)

    0 讨论(0)
  • 2021-01-30 01:16

    If you are using jQuery, use css to add CSS

    $("#voltaic_holder").css({'position': 'absolute',
        'top': '-75px'});
    

    To remove CSS attributes

    $("#voltaic_holder").css({'position': '',
        'top': ''});
    
    0 讨论(0)
  • 2021-01-30 01:21

    In case of .css method in jQuery for !important rule will not apply.

    In this case we should use .attr function.

    For Example: If you want to add style as below:

    <div id='voltaic_holder' style='position:absolute;top:-75px !important'>

    You should use:

    $("#voltaic_holder").attr("style", "position:absolute;top:-75px !important");

    Hope it helps some one.

    0 讨论(0)
  • 2021-01-30 01:24

    You could do any of the following

    Set each style property individually:

    $("#voltaic_holder").css("position", "relative");
    

    Set multiple style properties at once:

    $("#voltaic_holder").css({"position":"relative", "top":"-75px"});
    

    Remove a specific style:

    $("#voltaic_holder").css({"top": ""});
    // or
    $("#voltaic_holder").css("top", "");
    

    Remove the entire style attribute:

    $("#voltaic_holder").removeAttr("style")
    
    0 讨论(0)
提交回复
热议问题