jQuery插件正在应用内联样式(display:block)。我感觉很懒,想用display:none重写它。
最好的(懒惰的)方法是什么?
jQuery插件正在应用内联样式(display:block)。我感觉很懒,想用display:none重写它。
最好的(懒惰的)方法是什么?
当前回答
我有类似的问题与宽度属性。我不能从代码中删除!important,因为它在一个新模板上需要这种样式,我添加了一个Id (modalstyles)到元素中,然后我添加了以下代码到模板中:
<script type="text/javascript">
$('#modalstyling').css('width', ''); //Removal of width !important
$('#modalstyling').width('75%'); //Set custom width
</script>
其他回答
你可以使用jQuery的css方法设置样式:
$('something:visible').css('display', 'none');
你可以像这样创建一个jquery插件:
jQuery.fn.removeInlineCss = function (properties) {
if (properties == null) return this.removeAttr('style')
properties = properties.split(/\s+/)
return this.each(function () {
for (var i = 0; i < properties.length; i++)
this.style.removeProperty(properties[i])
})
}
使用
$(".foo").removeInlineCss(); //remove all inline styles
$(".foo").removeInlineCss("display"); //remove one inline style
$(".foo").removeInlineCss("color font-size font-weight"); //remove several inline styles
$("#element").css({display: "none"});
起初,我试图删除css中的内联样式,但它不起作用,新样式如display: none将被覆盖。但在JQuery中,它是这样工作的。
将插件更改为不再应用该样式。这比随后删除样式要好得多。
$('div[style*=block]').removeAttr('style');