Use jQuery to remove an inline style

后端 未结 6 1088
我在风中等你
我在风中等你 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/

提交回复
热议问题