【问题标题】:reset inline-css that has been set (only) by jQuery [duplicate]重置已由 jQuery 设置(仅)的内联 CSS [重复]
【发布时间】:2017-02-12 18:20:18
【问题描述】:

我有(请不要争辩)以下html

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

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

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

这让我在检查器中留下以下标记

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

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

我想出的唯一解决方案是通过.data() 存储初始的position-value,然后再次反转设置。

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

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

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

我想要什么:

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

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

我更多的是寻找一种干净的方法来解决这个问题,而不是在这里解决我的具体问题

【问题讨论】:

  • 找出它应该在你的班级变化中处于什么位置并将其更改为那个位置?

标签: jquery html css


【解决方案1】:

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

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

【讨论】:

  • 我向你发誓我试过了;它对我不起作用——但现在它起作用了!好吧,现在至少在网络上有证据。我在网上找不到关于它的信息。那么有什么更好的标题建议吗? [再次编辑] Stack 刚刚告诉我这个问题已经得到解答——我质疑我的谷歌技能:X
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 2020-06-06
  • 1970-01-01
  • 2013-12-04
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多