Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all inline style tage from HTML using dom

Tags:

dom

css

php

I have a dom and in this i have loaded whole of the webpage html. I want to remove all the inline style from all the elements in this page. How can i do this.

like image 358
Hacker Avatar asked Dec 31 '25 19:12

Hacker


2 Answers

var all = document.getElementsByTagName("*");

for (var i=0, max=all.length; i < max; i++) {
     // Do something with the element here
     all[i].removeAttribute("style","")
}
like image 64
Raab Avatar answered Jan 03 '26 10:01

Raab


Using DOM is the simpliest way.

Here's what I would do.

  1. Load your markup using loadHTML().
  2. Select all elements with the styles attribute using DOMXPath.
  3. Remove the styles attribute using removeAttribute() on each DOMElement.
like image 28
F21 Avatar answered Jan 03 '26 12:01

F21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!