求教:一个初级的setattribute()函数设置属性的代码为什么没有作用?

latintree 2015-05-09 06:55:15
我只想测试这个setattribute()函数来替换原来<img>标签里的title属性值,这段代码为什么不起作用?哪个地方写错了?请指教,谢谢了。


<script>
window.onload = www;
function www(){
var sc = document.getElementById("kkk");
sc.onmouseover = th;
}
function th(){
sc.setAttribute("title","22222");
}

</script>
</head>

<body>
<div class="ddd">
<ul>
<li><img id="kkk" src="../jpeg/mina590x343tn18gk6r.jpg" title="111111" /></li>
</ul>
</div>


</body>
...全文
111 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
latintree 2015-05-09
  • 打赏
  • 举报
回复
明白了,非常感谢
天际的海浪 2015-05-09
  • 打赏
  • 举报
回复
方式一

window.onload = www;
var sc;
function www(){	
	sc = document.getElementById("kkk");
	sc.onmouseover = th;	
}
function th(){	
	sc.setAttribute("title","22222");
}
方式二

window.onload = www;
function www(){	
	var sc = document.getElementById("kkk");
	sc.onmouseover = function(){	
		sc.setAttribute("title","22222");
	}
}
方式三

window.onload = www;
function www(){	
	var sc = document.getElementById("kkk");
	sc.onmouseover = th;	
}
function th(){	
	this.setAttribute("title","22222");
}

latintree 2015-05-09
  • 打赏
  • 举报
回复
谢谢,我把函数在里面有重新加了一句: <script> window.onload = www; function www(){ var sc = document.getElementById("kkk"); sc.onmouseover = th; } function th(){ var sc = document.getElementById("kkk"); sc.setAttribute("title","22222"); } </script> 好用了. 但是这样写总感觉是重复代码, 有没有更好的书写格式?
天际的海浪 2015-05-09
  • 打赏
  • 举报
回复
sc是www()函数中的局部变量,在th()函数的访问不到
相关推荐
发帖
JavaScript

8.7w+

社区成员

Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
帖子事件
创建了帖子
2015-05-09 06:55
社区公告
暂无公告