代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>getElementsByTagName</title> <script type="text/javascript"> 'use strict'; window.onload = function() { let buttonsForTagName = document.getElementsByTagName('button'); // 0 alert(buttonsForTagName.length); document.body.innerHTML = '<button id="button1">button1</button><button id="button2">button2</button><button id="button3">button3</button>'; // 3 alert(buttonsForTagName.length); } </script> </head> <body> </body> </html>
备注:
getElementsByTagName()方法一个动态方法。