多个“document.getElementById"脚本不起作用
Multiple "document.getElementById" scripts doesn't work
当我使用HTML #1时,多个id工作正常,但当我使用HTML #2时不工作。Chrome web控制台显示
>> "Uncaught TypeError: Cannot call method 'setAttribute' of null ".
<div id="0001"><a href="#"">LOGIN</a></div>
,浏览器不再工作。
HTML # 1
<html>
<head>
<title>Index</title>
</head>
<body>
<div id="0001"><a href="#"">LOGIN</a></div>
<div id="0002"><a href="#"">GO</a></div>
<div id="0003"><a href="#"">VISIT</a></div>
<div id="0004"><a href="#"">EXPLORE</a></div>
<div id="0005"><a href="#"">FUNNY PICS</a></div>
<script>
var element = document.getElementById("0001");
element.setAttribute('onclick', 'window.location.href=''http://site.com/1.html''');
var element = document.getElementById("0002");
element.setAttribute('onclick', 'window.location.href=''http://site.com/2.html''');
var element = document.getElementById("0003");
element.setAttribute('onclick', 'window.location.href=''http://site.com/3.html''');
var element = document.getElementById("0004");
element.setAttribute('onclick', 'window.location.href=''http://site.com/4.html''');
var element = document.getElementById("0005");
element.setAttribute('onclick', 'window.location.href=''http://site.com/5.html''');
</script>
</body>
</html>
HTML # 2
<html>
<head>
<title>Index</title>
</head>
<body>
<div id="0002"><a href="#"">GO</a></div>
<div id="0003"><a href="#"">VISIT</a></div>
<div id="0004"><a href="#"">EXPLORE</a></div>
<div id="0005"><a href="#"">FUNNY PICS</a></div>
<script>
var element = document.getElementById("0001");
element.setAttribute('onclick', 'window.location.href=''http://site.com/1.html''');
var element = document.getElementById("0002");
element.setAttribute('onclick', 'window.location.href=''http://site.com/2.html''');
var element = document.getElementById("0003");
element.setAttribute('onclick', 'window.location.href=''http://site.com/3.html''');
var element = document.getElementById("0004");
element.setAttribute('onclick', 'window.location.href=''http://site.com/4.html''');
var element = document.getElementById("0005");
element.setAttribute('onclick', 'window.location.href=''http://site.com/5.html''');
</script>
</body>
</html>
整个概念很差。如果你有链接,使用链接。直接没有脚本(适用于所有浏览器和SEO)或更改链接
的onclick的href。也不要有数字id
演示<html>
<head>
<title>Index</title>
<script type="test/javascript">
var myLinks = [
'http://bing.com/search?q=go',
'http://bing.com/search?q=visit',
'http://bing.com/search?q=explore',
'http://bing.com/search?q=funny%20pics'
]; // note no comma on the last item
window.onload=function() {
for (var i=0;i<myLinks.length;i++) {
// EITHER document.getElementById("d"+i).getElementsByTagName("a")[0]).href=myLinks[i];
// OR
document.getElementById("d"+i).getElementsByTagName("a")[0].onclick=(function(idx) {
var idx = i;
return function() { // closure
location=myLinks[idx];
return false; // cancel href
}
})(i);
}
}
</script>
</head>
<body>
<div id="d0"><a href="#"">GO</a></div>
<div id="d1"><a href="#"">VISIT</a></div>
<div id="d2"><a href="#"">EXPLORE</a></div>
<div id="d3"><a href="#"">FUNNY PICS</a></div>
</body>
</html>
您需要首先检查该元素是否为null。
HTML # 2
<html>
<head>
<title>Index</title>
</head>
<body>
<div id="0002"><a href="#"">GO</a></div>
<div id="0003"><a href="#"">VISIT</a></div>
<div id="0004"><a href="#"">EXPLORE</a></div>
<div id="0005"><a href="#"">FUNNY PICS</a></div>
<script>
var element = document.getElementById("0001");
if(element!=null)
{
element.setAttribute('onclick', 'window.location.href=''http://site.com/1.html''');
}
var element = document.getElementById("0002");
if(element!=null)
{
element.setAttribute('onclick', 'window.location.href=''http://site.com/2.html''');
}
var element = document.getElementById("0003");
if(element!=null)
{
element.setAttribute('onclick', 'window.location.href=''http://site.com/3.html''');
}
var element = document.getElementById("0004");
if(element!=null)
{
element.setAttribute('onclick', 'window.location.href=''http://site.com/4.html''');
}
var element = document.getElementById("0005");
if(element!=null)
{
element.setAttribute('onclick', 'window.location.href=''http://site.com/5.html''');
}
</script>
</body>
</html>
相关文章:
- 当有许多形式时,Fancybox脚本不起作用
- Aureliaview中的脚本不起作用
- 淡入淡出脚本不起作用
- 从jquery对话框恢复原始数据,脚本不起作用
- 我的脚本不起作用
- 用于wooccommerce的简单JQuery脚本不起作用
- 简单的拖放JS脚本不起作用
- jquery和JavaScript脚本不起作用
- jquery脚本不起作用
- 为什么这个jquery脚本不起作用
- 带有ajax xml请求的Node js脚本不起作用
- 示例Casper JS脚本不起作用
- 多个脚本不起作用
- JavaScript/jQuery-脚本不起作用-添加&删除类,合并数组-测试用例
- 在php中验证字段时,java脚本不起作用
- 只有在条件满足时才加载javascript脚本-不起作用
- 使用另一个 ajax 脚本更改页面内容后,AJAX 脚本不起作用
- 网络视图的电子预加载脚本不起作用
- 用于显示服务器上的 javascript 时间的脚本不起作用
- “BBCODE”脚本不起作用