The relationship between setAttribute and different versions of ie in javascript

Lionsure 2020-06-18 Original by the website

Browser versions are constantly being updated and upgraded, and some javascript methods that are not supported by lower browsers are also supported in higher versions. This is why some javascript code can be executed normally in some high-level browsers, but the error is reported in the low-level browsers or compatibility mode of high-level browsers.

Take javascript to set html element attribute. The setAttribute method is not supported by some low-level Internet Explorer browsers, and other methods must be used, otherwise an error will be reported.

 

1. Ie version supporting setAttribute method

The setAttribute method is only supported in versions above ie8, neither ie6 nor ie7. The following code can be executed normally in versions above ie8, ie6 and ie7 report errors:

<div id="pageid"></div>
       var p = document.getElementById("pageid");
       p.setAttribute("style", "width:90%");

The above code uses a percentage to determine the width of the webpage, mainly using the setAttribute() method of javascript to set the style attribute of div to determine the width.

 

2. Use javascript to set the width of the html element for ie6 and ie7

Before setAttribute() came out, how did javascript set the width of html elements? The web front-end setter who is familiar with ie6 must be familiar with "obj.style.width =", which was used to set html element attributes. Since this method supports the lower version of ie, the higher version of ie is also supported, because the program has the habit of backward compatibility. The specific setting code is as follows:

<div id="pageid"></div>
       var p = document.getElementById("pageid");
       p.style.width = "88%";