IE8下关于querySelectorAll()的问题

(编辑:jimmy 日期: 2025/9/25 浏览:2)

当用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>
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。