日期:2014-05-16 浏览次数:20396 次
文章背景说明:本人在开发时遇到的问题,在Iteye问答里提问,本文是在朋友们帮忙的回答中总结的。谢谢他们。
http://www.iteye.com/problems/88349
?
?
?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>About</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
</head>
<body >
<table width="100%">
<tr><a href="#" onClick="alert('123');" >123明细信息</a></tr>
<tr><a href="#" onClick="alert('456');" >456明细信息</a></tr>
<tr><a href="#" onClick="alert('789');" >789明细信息</a></tr>
<table>
</body>
<script language="javascript">
function initHrefColor(objs){
var objs=document.getElementsByTagName("a");
outer:for(var i=0;i<objs.length;i++){
(function (e) {
e.attachEvent("onclick", function () { changeHrefStytle(e); });
})(objs[i]);
}}
function changeHrefStytle(obj){
obj.style.color='green';
// if(!obj.innerText.contains('√')){
obj.innerHTML = '√ ' + obj.innerText;
// }
}
initHrefColor();
</script>
</html>
?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>About</title>
<meta http-equiv=Content-Type content="text/html; charset=utf8">
</head>
<body >
<table width="100%" border="1">
<tr><td><a href="#" >123明细信息</a></td></tr>
<tr><td><a href="#" >456明细信息</a></td></tr>
<tr><td><a href="#" >789明细信息</a></td></tr>
<tr><td><text>ss</text></td></tr>
<table>
</body>
<script language="javascript">
function initHrefColor(){
var objs=document.getElementsByTagName("a");
var obj=null;
outer:for(var i=0;i<objs.length;i++){
obj=objs[i];
//非Mozilla系列
if(obj.attachEvent) {
obj.attachEvent("onclick",aaa);
} else {
obj.addEventListener("click",aaa);
}
}}
function aaa(event){
var src = event.srcElement;
if(!src) {
src = event.target;
}
src.style.color='green';
//if(!src.innerText.contains('√')){
src.innerHTML = '√ ' + src.innerText;
//}
}
initHrefColor();
</script>
</html>
?
?
在js编写规范里,提到将js写到head里是最好的。
但是你这里var objs=document.getElementsByTagName("a");
如果写在head里肯定拿不到,所以要加上window.onload去执行。
?
网友提供的几个js学习网站(或者是文章链接):
?
http://bonsaiden.github.com/JavaScript-Garden/zh/
?
http://www.cnblogs.com/terryglp/articles/1776695.html
?
http://codex.wordpress.org.cn/HTML%E5%85%B3%E4%BA%8EEvent%E5%AF%B9%E8%B1%A1
?