Use jQuery to remove an inline style

后端 未结 6 944
我在风中等你
我在风中等你 2021-01-17 10:44
    ...

Is it possible with jQuery (or plain javascript) to remove ju

相关标签:
6条回答
  • 2021-01-17 10:54

    Yes! You can!

    If you set the css property to a blank value, it clears it.

    So, if your inline style is "background: pink" you can remove it via:

    $element.css('background','')
    

    I'm pretty sure I learned that somewhere here on SE but, alas, don't have the specific source to site, so apologies for not being able to give credit where credit is due.

    UPDATE:

    Well, I suppose I should have gone to the source. From the jQuery documentation (emphasis mine):

    Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element.

    http://api.jquery.com/css/

    0 讨论(0)
  • 2021-01-17 11:01

    add your style attribute to CSS and then you can add or remove class with these jQuery functions: addClass and removeClass

    0 讨论(0)
  • 2021-01-17 11:05

    try this:

    $('.mydiv ul').height('');
    
    0 讨论(0)
  • 2021-01-17 11:08

    Yes it is .. dpeneding on how you'd like to do it

    <script type="text/javascript">
        function changestyle(element) {
            var currentstyle = document.getElementById(element).style.display;
            if (currentstyle == "inline") {
                document.getElementById(element).style.display = "";
                } else {
                document.getElementById(element).style.display = "inline";
            }
        }
    </script>
    

    this may vary slightly for different broswers but it should show you how to change it if you want.

    eek .. disregard .. jquery - much easier :)

    0 讨论(0)
  • 2021-01-17 11:12

    To remove an attribute, use this:

    $(".mydiv UL").css("height", "");
    

    Thanks to am not i am for correcting my answer.

    0 讨论(0)
  • 2021-01-17 11:16
    $('.mydiv ul').each(function(){
        $(this).attr('style', $(this).attr('style').replace('height: ', 'height:').replace('height:'+$(this).css('height')+';', '')); 
    });
    

    just make sure the style value format either "height:111px;" or "height: 111px;"

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