simplest way to remove all the styles in a page

后端 未结 4 1536
礼貌的吻别
礼貌的吻别 2020-12-05 03:36

I need to remove all the style definitions in a page using javascript, to obtain the same result as doing View > Page Style > No Style in Firefox. Which i

4条回答
  •  执笔经年
    2020-12-05 03:59

    Here is the ES6 goodness you can do with just one line.

    1) To remove all inline styles (eg: style="widh:100px")

    document.querySelectorAll('[style]')
      .forEach(el => el.removeAttribute('style'));
    

    2) To remove link external stylesheet (eg: )

    document.querySelectorAll('link[rel="stylesheet"]')
      .forEach(el => el.parentNode.removeChild(el));
    

    3) To remove all inline style tags (eg: )

    document.querySelectorAll('style')
      .forEach(el => el.parentNode.removeChild(el));
    

提交回复
热议问题