我想从 div 中删除一些元素属性。我的 div 是自动生成的。我想遍历每个 div 和子 div,并想删除所有 font-size (font-size: Xpx)
和 size
里面font tag <font size="X">
.
这些是我的 div 的例子。
<div id="headline" class="headline" style="position: word-break: break-all; background-color: rgb(90, 88, 84); width:300px;height:250px">
<div style="text-align: center; font-size: 12px; color: rgb(1, 69, 18);">
<div>
<div>
<font color="#ffffff" size="6" >
<b>This is </b>
</font>
<b style="color: rgb(255, 255, 255); font-size: xx-large;">a Test </b>
</div>
</div>
<div>
<div>
<b style="color: rgb(255, 255, 255); font-size: xx-large;">Demo </b>
</div>
</div>
<div>
<b style="color: rgb(255, 255, 255); font-size: xx-large;">csa</b>
</div>
</div>
</div>
或
<div id="headline" class="headline" style="position: absolute; width: 186px; height: 50px; ">
<div style="text-align: center; font-size: 12px; color: rgb(249, 249, 251);">
<span style="color: rgb(255, 204, 153);">
<font size="4">Expansion Pack</font>
</span>
</div>
</div>
我正在阅读 div。
$('#headline').find('*').each(function() {
// font-size remove
//size inside tag remove
});
但是如果它们在子元素中,我如何删除字体大小和大小,有时只有字体大小存在而没有 <font>
标签
最佳答案
我认为您需要更明确地选择您的选择器。这样的事情应该有效:
// Select all elements with a style attribute that contains font-size, and
// remove the inline font-size property
$('[style*="font-size"]').css('font-size', '');
// Select all font elements with a size attribute, and
// remove the size attribute
$('font[size]').removeAttr('size');
您可能希望使选择器更加具体,以避免意外修改内容。例如:
$('#headline [style*="font-size"]').css('font-size', '');
注意:此解决方案使用 jQuery attribute selectors .
编辑:查看 Charlie 关于性能的评论。属性选择器比大多数其他选择器慢得多,但我敢打赌,在大多数用例中,速度差异无关紧要。
关于javascript - 从子元素中删除 Size 和 font-size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30609784/