Detailed explanation of getElementsByName in JavaScript and matters needing attention in IE

  • 2021-07-22 08:40:57
  • OfStack

Detailed explanation of getElementsByName in JavaScript and matters needing attention in IE

Foreword:

In IE5-9, the getElementsByClassName () method that does not implement js is implemented, but the getElementsByName () method is implemented, but it should be noted that this method in IE5-9 also returns the specified element that matches the id attribute. For compatibility, it should be used carefully, and the same string should not be used as both the name and ID.

The test procedure is as follows:


  <div id="log">
      <div id="innerLog">
      </div>
    </div>

 function testCSSSelector() {
 console.log(document.getElementsByName("log").length);
               console.log(document.getElementsByName("innerLog").length);
      };

After testing, all of them output 1 in IE5-9 browsers, and all of them output 0 above IE10 (including IE10 and Edge browsers). The output of google and Firefox is also 0. If you are using 360 browsing, some of its modes are also using the IE kernel, and the output is also 1.

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: