jQuery插件正在应用内联样式(display:block)。我感觉很懒,想用display:none重写它。

最好的(懒惰的)方法是什么?


当前回答

下面是我编写的插入jQuery的inlineStyle选择器过滤器。

$("div:inlineStyle(display:block)") // will select all divs with an inline style of display: block set

在你的例子中,你可以这样使用:

$("div:inlineStyle(display:block)").hide();

其他回答

你可以使用jQuery的css方法设置样式:

$('something:visible').css('display', 'none');

将插件更改为不再应用该样式。这比随后删除样式要好得多。

删除内联样式(由jQuery生成)最简单的方法是:

$(this).attr("style", "");

内联代码应该消失,对象应该适应CSS文件中预定义的样式。

为我工作!

.removeAttr("style")来去掉整个style标签…

.attr("style")来测试值,看看是否存在内联样式…

.attr("style",newValue)将其设置为其他值

你可以像这样创建一个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