how to remove css property using javascript?

前端 未结 10 1711
醉话见心
醉话见心 2020-11-27 12:46

is it possible to remove a CSS property of an element using JavaScript ? e.g. I have div.style.zoom = 1.2, now i want to remove the zoom property through JavaS

相关标签:
10条回答
  • 2020-11-27 13:02

    You have two options:

    OPTION 1:

    You can use removeProperty method. It will remove a style from an element.

    el.style.removeProperty('zoom');
    

    OPTION 2:

    You can set it to the default value:

    el.style.zoom = "";
    

    The effective zoom will now be whatever follows from the definitions set in the stylesheets (through link and style tags). So this syntax will only modify the local style of this element.

    0 讨论(0)
  • 2020-11-27 13:02

    You can try finding all elements that have this class and setting the "zoom" property to "nothing".

    If you are using jQuery javascript library, you can do it with $(".the_required_class").css("zoom","")

    Edit: Removed this statement as it turned out to not be true, as pointed out in a comment and other answers it has indeed been possible since 2010.

    False: there is no generally known way for modifying stylesheets from JavaScript.

    0 讨论(0)
  • 2020-11-27 13:04

    To change all classes for an element:

    document.getElementById("ElementID").className = "CssClass";
    

    To add an additional class to an element:

    document.getElementById("ElementID").className += " CssClass";
    

    To check if a class is already applied to an element:

    if ( document.getElementById("ElementID").className.match(/(?:^|\s)CssClass(?!\S)/) )
    
    0 讨论(0)
  • 2020-11-27 13:08

    You can use the styleSheets object:

    document.styleSheets[0].cssRules[0].style.removeProperty("zoom");
    

    Caveat #1: You have to know the index of your stylesheet and the index of your rule.

    Caveat #2: This object is implemented inconsistently by the browsers; what works in one may not work in the others.

    0 讨论(0)
  • 2020-11-27 13:11

    This should do the trick - setting the inline style to normal for zoom:

    $('div').attr("style", "zoom:normal;");

    0 讨论(0)
  • 2020-11-27 13:19
    element.style.height = null;
    

    output:

    <div style="height:100px;"> 
    // results: 
    <div style="">
    
    0 讨论(0)
提交回复
热议问题