当前位置:Gxlcms > JavaScript > IE8下关于querySelectorAll()的问题_javascript技巧

IE8下关于querySelectorAll()的问题_javascript技巧

时间:2021-07-01 10:21:17 帮助过:6人阅读

当用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页面开始一定要记得声明。

测试代码:


代码如下:






This is a sample warning
This is a sample error


This is another sample warning
This is another sample error

输出:2
var b = document.querySelector("[id=3err]")
alert(b.tagName)//报错
var c = document.querySelectorAll("[name=1err]")
alert(c.length)//报错



人气教程排行