简体   繁体   English

删除所有子元素的字体大小和其他内联 styles 并替换

[英]Remove font size and other inline styles of all child elements and replace

The page will contain random divs, p, span, and other nested elements inside divs with a known class (summary).该页面将包含随机 div、p、span 和 div 内的其他嵌套元素,其中已知 class(摘要)。 This data is being pasted from word and other programs, so might contain styling that I want to override.此数据是从 word 和其他程序中粘贴的,因此可能包含我想要覆盖的样式。 Since there are multiple levels and the name of the next div is random, .important doesn't work to try to override the inline styling, My thought is to use javascript to select all of the children, strip the styles.由于有多个级别并且下一个 div 的名称是随机的,因此 .important 无法尝试覆盖内联样式,我的想法是使用 javascript 到 select 所有子级,剥离 ZBC4150D023D32551D36ZAC79。 then apply my own?然后申请我自己的? Is this possible, If so?这可能吗,如果可以? can you give me an example of a function?你能给我一个函数的例子吗?

Here's a sample of the kind of code that's on the page:这是页面上的代码示例:

  <div class="ExternalClass251DEFD3BA2542E59F9D13DD7BCEC24A">
    <div style="box-sizing:border-box;font-family:&quot;Segoe UI&quot;, system-ui, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, sans-serif;font-size:14.6667px;orphans:2;widows:2;">
      <span style="font-size:10.5pt;">
        <span>
          <span>
            <span>
              <span>This web page was updated to add the <i>other random text </i>
                <span style="font-size:11pt;">
                  <span>
                    <span style="font-size:10.5pt;">
                      <span>
                        <span>
                          <span>
                            <span>more text here </span>
                          </span>
                        </span>
                      </span>
                    </span>
                  </span>
                </span>
                and some <b>other text here</b> section.
              </span>
            </span>
          </span>
          Here is the summary of
        </span>
      </span>
      <span>
        the property evaluation process. 
      </span>
    </div>
  </div>
</div>

Here's what I have tried:这是我尝试过的:

function removeStyles(el) {
    el.removeAttribute('style');

    if(el.childNodes.length > 0) {
        for(let child in el.childNodes) {
            /* filter element nodes only */
            if(el.childNodes[child].nodeType == 1)
                removeStyles(el.childNodes[child]);
        }
    }
}

But this removes all of the styling, whereas I'm just wanting to remove it from within certain divs.但这会删除所有样式,而我只是想从某些 div 中删除它。

First of all, let's assume that all of the tags that you're going to get will be inside a div called .container .首先,让我们假设您要获取的所有标签都将位于名为.container的 div 中。

And we'll assume that the elements which will be found inside the .container are a mix of elements, ie: <p> , <span> , <div> , <ul> , <li> , etc..etc...我们假设在.container中找到的元素是元素的混合,即: <p><span><div><ul><li>等等。 .

You can use the query.selectorAll method which returns a node list , use example as follows:您可以使用返回节点列表query.selectorAll方法,使用示例如下:

 // 1. get all the elements inside.container and spread them in an array (NodeList --> Array) const elements = [...document.querySelectorAll('.container *')] // 2. apply the style x for all the elements elements.forEach((el) => { el.style.color = "red" })
 <div class="container"> <div> nested div#1 layer 1 </div> <div> nested div#2 layer 1 <p> nested p layer 2 </p> </div> <div> nested div#3 layer 1 <div> nested div layer 2 <div> nested div layer 3 <div> nested div layer 4 <span>nested span#1 layer 4</span> <span>nested span#2 layer 4</span> <span>nested span#3 layer 4</span> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM