复制代码 代码如下: <!DOCTYPE> <html> <head></head> <body> <div id="foo"> <a name="warns" href="">This is a sample warning</a> <a id="3err" href="">This is a sample error</a> </div> <div id="bar"> <a name="warns" href="">This is another sample warning</a> <a name="1err" href="">This is another sample error</a> </div> <script> var a = document.querySelectorAll("[name=warns]") alert(a.length)//输出:2 var b = document.querySelector("[id=3err]") alert(b.tagName)//报错 var c = document.querySelectorAll("[name=1err]") alert(c.length)//报错 </script> </body> </html>