【发布时间】:2016-01-11 12:25:22
【问题描述】:
【问题讨论】:
-
为什么不直接编辑 HTML?
-
因为我做不到。 html 由产品供应商提供。但我可以编辑一个 css 文件进行自定义
【问题讨论】:
【讨论】:
!important的唯一理由
您可以使用 JavaScript 禁用任何给定元素的内联样式:
style属性var button = document.getElementById('myButton');
button.removeAttribute('style');
【讨论】:
style 属性替换为自定义 data-style 属性(或类似属性)将是最好的解决方案。
如果现在其他人偶然发现了这一点,您可以做的不仅仅是使用 JavaScript 删除内联 HTML 样式标签。您可以保留标签以备日后退回。有两种方法可以做到!
我听说在 HTML 标记上使用 disabled 会阻止浏览器处理它。但我很难让它在 Firefox 上运行。也许有人可以启发我!
<style class='style-class' disabled>
body { color: blue; }
</style>
所以你只需要
$('.style-class').prop('disabled', true)
您的浏览器只会解析样式标签中的样式。因此,如果您将标签更改为其他任何内容,您仍然可以在 DOM 中检查它,但它不会将其视为样式表。
(这里用jQuery来解释)
$(style.selector).replaceWith (function () {
var attributes = $(this).prop("attributes");
var $newEl = $('<nostyle>')
$.each(attributes, function() {
$newEl.attr(this.name, this.value);
});
return $newEl.html($(this).html())
});
然后当你准备好返回样式时,使用这个:
$(notstyle.selector).replaceWith (function () {
var attributes = $(this).prop("attributes");
var $newEl = $('<style>')
$.each(attributes, function() {
$newEl.attr(this.name, this.value);
});
return $newEl.html($(this).html())
});
【讨论】:
!important,不需要 JavaScript,当然也不需要使用 JavaScript 库的多个复杂函数。
document.querySelector('#myButton').removeAttribute('style'); 这样的单行JS,而不是在我的CSS 中添加!important。我倾向于认为后者只是在开发过程中用作临时作弊模式。
!important 的“单个词”要长得多)。