簡體   English   中英

從子元素中刪除大小和字體大小

[英]Removing Size and font-size from child elements

我想從div中刪除一些元素屬性。 我的div是自動生成的。 我想遍歷每個div和子div,並要刪除font tag <font size="X">內的所有font-size (font-size: Xpx)size

這些是我的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&nbsp;</b>
        </font>
        <b style="color: rgb(255, 255, 255); font-size: xx-large;">a Test&nbsp;</b>
      </div>
    </div>
    <div>
      <div>
        <b style="color: rgb(255, 255, 255); font-size: xx-large;">Demo&nbsp;</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>

我正在閱讀divs一樣。

$('#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屬性選擇器

編輯:請參閱查理關於性能的評論。 屬性選擇器比大多數其他選擇器慢得多,但是我敢打賭,在大多數情況下,速度差異並不重要。

嘗試循環獲得“ font-size”的那些元素:

$('div').each(function(){
    $(this).css('font-size', '');  // set the value as empty string
)};

$('b').each(function(){
    $(this).css('font-size', ''); 
)};

請參閱CSS上的jQuery文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM