当用querySelector()或querySelectorAll()查找类似name="2nd_btn"的元素时,FF,chrome和IE8都会报错。 FF,chrome报的错是一样的,如下所示: Error: uncaught exception: [Exception... "An invalid or illegal string was specified" code: "12" nsresult: "0x8053000c (NS_ERROR_DOM_SYNTAX_ERR)" location: ".../test/qsa.html Line: 18"] IE8的报错提示:行: 18 错误: 参数无效。 一寻思,name的值是以数字开头的,把数字去掉或修改后,就能取到了。 这就告诉我们,一般可自定义标签的属性值时,属性值不能以数字开头,也不能包含$,^等不常用的字符。 在HTML页面开始一定要记得声明<!DOCTYPE>。 测试代码: 复制代码 代码如下: <!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>
推荐阅读
JavaScript 判断指定字符串是否为有效数字
这样的check以前已经碰到很多了,但每次都是解决了就完了,没有记录下来,等下次碰到时又要花很多的时间去写。东西是需要积累的,现在特记录下来,希望这样的工作不再重复太多,也与大家一起分享。 复制代码 代码如>>>详细阅读
本文标题:IE8下关于querySelectorAll()的问题
地址:http://www.17bianji.com/kaifa2/JS/26428.html
1/2 1