jQuery插件正在应用内联样式(display:block)。我感觉很懒,想用display:none重写它。
最好的(懒惰的)方法是什么?
jQuery插件正在应用内联样式(display:block)。我感觉很懒,想用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
其他回答
$("[style*=block]").hide();
如果你想在一个样式属性中删除一个属性——而不仅仅是将它设置为其他属性——你可以使用removeProperty()方法:
document.getElementById('element').style.removeProperty('display');
更新: 我做了一个交互式的小提琴来玩不同的方法和他们的结果。显然,set to empty string、set to faux value和removeProperty()之间没有太大区别。它们都会导致相同的回退——要么是预先给定的CSS规则,要么是浏览器的默认值。
我有类似的问题与宽度属性。我不能从代码中删除!important,因为它在一个新模板上需要这种样式,我添加了一个Id (modalstyles)到元素中,然后我添加了以下代码到模板中:
<script type="text/javascript">
$('#modalstyling').css('width', ''); //Removal of width !important
$('#modalstyling').width('75%'); //Set custom width
</script>
.removeAttr("style")来去掉整个style标签…
.attr("style")来测试值,看看是否存在内联样式…
.attr("style",newValue)将其设置为其他值
如果显示是你的样式中唯一的参数,你可以运行这个命令来删除所有的样式:
$('#element').attr('style','');
这意味着如果你的元素之前是这样的:
<input id="element" style="display: block;">
现在你的元素看起来是这样的:
<input id="element" style="">