Remove CSS “top” and “left” attributes with jQuery

后端 未结 13 1209
故里飘歌
故里飘歌 2020-12-02 07:07

Im building a draggable map that when the map is dragged the element is given a \'left\' and \'top\' attribute with values for each as so...

相关标签:
13条回答
  • 2020-12-02 07:46

    If you want to remove all of it you can do

    $('.map').removeAttr('style');
    
    0 讨论(0)
  • 2020-12-02 07:47

    There are some styles that can't be easily remove.(overflow comes to mind)

    A workaround would be to create 2 different classes and add/remove the one containing the style you want.

    NOT the best solution for style properties that can be easily remove/disabled.

    0 讨论(0)
  • 2020-12-02 07:47

    To remove css styles assign an empty string to the style

    in this case

    $('.map').css({top:'',left:''});
    
    0 讨论(0)
  • 2020-12-02 07:50

    Per this JQuery bug report

    element.removeAttr('style')
    doesn't work consistently in Webkit based browsers. For example, I ran across this problem on iOS 6.1. The fix is to use:
    element.attr('style', '')

    0 讨论(0)
  • 2020-12-02 07:52
    $.fn.removeCss=function(prop){
       if(!prop){
              return $(this).removeAttr('style').removeAttr('class');
         }
        else{
             return $(this).css(prop,null);
        }
    
    }
    

    then if you want to remove css prop(s) :

        $('#mydiv').removeCss('color');
    //To remove all prop Css
        $('#mydiv').removeCss();
    
    0 讨论(0)
  • 2020-12-02 07:55
    1. Go here: jQuery API
    2. Ctrl + F for 'remove'
    3. Read:

    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.

    The docs give you the current, recommended approach for what you're trying to accomplish, which can often save you time because you don't have to read back and forth flame wars on stack overflow (no matter how fun/enlightening that may be!).

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