📜  如何使用 jQuery 删除 CSS“top”和“left”属性?

📅  最后修改于: 2022-05-13 01:55:57.085000             🧑  作者: Mango

如何使用 jQuery 删除 CSS“top”和“left”属性?

方法一:使用 css() 方法: css() 方法用于获取或设置所选元素的 CSS 属性。它有两个参数,第一个参数是必须设置的属性,第二个参数是它必须设置的值。

通过使用将属性设置为其默认值的“初始”值,可以有效地禁用“顶部”值。这与 'left' 值类似。这将消除元素中 'top' 和 'left' 值的影响。

句法:

function removeTopLeft() {
    $('selectedElement').css('top', 'initial');
    $('selectedElement').css('left', 'initial');
}

例子:



  

    
        How to Remove CSS "top" and
        "left" attribute with jQuery?
    
      
    
      
    

  

    

        GeeksForGeeks     

                    How to Remove CSS “top” and         “left” attribute with jQuery?                 

        Click on the button to remove         the top and left attribute.     

                       
              

输出:



  • 点击按钮前:
    css-before
  • 点击按钮后:
    css-after

方法 2:使用 top 和 left 属性:在元素的 style 属性的帮助下访问这些属性可以更改 top 和 left 值。首先使用 jQuery 选择器选择所需元素。然后使用所选对象上的点表示法访问样式属性及其“顶部”和“左侧”值。

通过使用将属性设置为其默认值的“初始”值,可以有效地禁用“顶部”值。 'left' 值也是如此。这将消除元素中 'top' 和 'left' 值的影响。

句法:

function removeTopLeft() {
    $('selectedElement')[0].style.top = "initial";
    $('selectedElement')[0].style.left = "initial";
}

例子:



      

    
        How to Remove CSS "top" and
        "left" attribute with jQuery?
    
      
    
      
    

  

    

        GeeksForGeeks     

                    How to Remove CSS “top” and         “left” attribute with jQuery?                 

        Click on the button to remove         the top and left attribute.     

                       
              

输出:

  • 点击按钮前:
    样式属性之前
  • 点击按钮后:
    风格属性之后