重置由jQuery设置(仅)的inline-css

时间:2016-10-04 14:20:08

标签: jquery html css

我有(请不要对抗原因)以下html

<div class="something" style="background: green;"></div>

现在需要通过jQuery添加position: fixed

$(".something").css("position", "fixed");

在检查员

中留下了以下标记
<div class="something" style="background: green; position: fixed"></div>

不,我想再次从; position: fixed - 属性中删除style

我提出的唯一解决方案是通过position存储初始.data() - 值,然后再将其设置为反转设置。

但是这给我留下了以下标记..

<div class="something" style="background: green; position: static"></div>

不幸的是,之后我无法在position - 属性中拥有style - 属性。它会覆盖我的css进行一些类更改等。

我想要的是什么:

我现在正在寻找一种方法来删除jQuery放在那里的style - 属性的部分...类似于$(".something").css("position", unset);

请不要为我的css提出!important建议!

我更希望找到一个干净的方法来解决这个问题,而不是解决我在这里遇到的具体问题

1 个答案:

答案 0 :(得分:2)

如果要删除特定的CSS样式属性,可以使用css()函数并将其设置为空字符串以将其删除:

// This will set the position attribute back to its original value
$('.something').css('position','');
相关问题