我有一个 div.innerDiv,它已在 CSS 文件中分配了一个 height: 300px !important;。我既不能更改现有的 CSS 文件,也不能添加要覆盖的新类。由于样式是 !important ,所以它可以被覆盖为 inline 样式,只有 !important 。但它不会产生预期的效果。

引用演示 here

更新:
我只需要 内联 样式。

HTML:

<div>
    <div class="innerDiv">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
        survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
        software like Aldus PageMaker including versions of Lorem Ipsum.
    </div>
</div>

CSS:
.innerDiv {
  height: 300px !important;
  width: 150px;
  border: 1px solid;
}

JS:
function overrideHeight() {
    document.getElementsByClassName('innerDiv')[0].style.height = 400 + 'px !important';
}

overrideHeight();

最佳答案

在我的浏览器(iPad Safari)中,Matt 的答案仅在修改为:

function overrideHeight() {
document.getElementsByClassName('innerDiv')[0].style.cssText = "height: 400px !important";
}

所以可能值得关注跨浏览器的怪癖。

关于javascript - 使用 javascript 无法使用 !important 覆盖高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35574506/

10-13 00:21