gpt4 book ai didi

jQuery:删除特定样式标签

转载 作者:行者123 更新时间:2023-11-28 09:18:29 25 4
gpt4 key购买 nike

我们的表单软件生成了许多内联 style 属性,我想将其删除。我知道这可以用 $('div[style]').removeAttr('style') 之类的东西很快地完成,但不幸的是,我需要保留所有 display:none 因为这些可以稍后显示,具体取决于条件选择。

基于 https://stackoverflow.com/questions/2465158/is-it-possible-to-remove-inline-styles-with-jquery ,我知道像 $('div').css('color', ''); 这样的东西会起作用,但我需要重复 font-size, text-align

那么,什么是更有效的方法(最好使用 jQuery)删除所有不是 display 的样式标签,最好是在链接/单行中陈述?我想过使用 :not 但无法弄清楚如何与上面的例子结合......

谢谢

最佳答案

您可以删除所有 style 属性,但保留任何现有的 display: none 标志,方法是使用您显示的选择器,然后检测 display: none 并在删除样式后重新声明它:

$("div[style]").each(function() {
var hide = this.style.display === "none";
$(this).removeAttr("style");
if (hide) {
this.style.display = "none";
}
});

或者更一般地说,保留 display 属性而不考虑其值:

$("div[style]").each(function() {
var display = this.style.display;
$(this).removeAttr("style");
this.style.display = display;
});

完整示例:Fiddle

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<meta charset=utf-8 />
<title>Style Update</title>
</head>
<body>
<div>No style</div>
<div style="color: green">Green</div>
<div style="color: red; display: none">Red, hidden</div>
<div><button type="button" id="processStyle">Process style attributes</button></div>
<div><button type="button" id="showHidden">Show hidden briefly</button></div>
<p>Click "Show hidden briefly" first, then "Process style attribuets", then "Show hidden briefly" again.</p>
<script>
$("#processStyle").click(function() {
$("div[style]").each(function() {
var display = this.style.display;
$(this).removeAttr("style");
this.style.display = display;
});
});
$("#showHidden").click(function() {
$("div").each(function() {
if (this.style.display === "none") {
$(this).fadeIn('fast').delay(1000).fadeOut('fast');
}
});
});
</script>
</body>
</html>

关于jQuery:删除特定样式标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22064779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com