87,923
社区成员
发帖
与我相关
我的任务
分享
<span id="c1" class="c">
xxx
</span>
<span id="c2" class="c">
yyy
</span>
<span id="c3" class="c">
zzz
</span>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function() {
var spans = document.getElementsByTagName("span");
for(var i = 0; i < spans.length; i++) {
spans[i].onclick = function() {
alert(this.id);
};
}
};
</script>
<style>
.c {
width:30px;
height:20px;
line-height:20px;
background-color:red;
cursor:pointer;
margin:3px;
}
</style>
</head>
<body>
<span id="c1" class="c">
xxx
</span>
<span id="c2" class="c">
yyy
</span>
<span id="c3" class="c">
zzz
</span>
</body>
</html>
function showClasName(obj){
alert(obj.id);
}
<span id="c1" class="c" onclick="showClasName(this)">
xxx
</span>
<span id="c2" class="c" onclick="showClasName(this)">
yyy
</span>
<span id="c3" class="c" onclick="showClasName(this)">
zzz
</span>