得到或设置触发事件的对象。
event.srcElement就是指向触发事件的元素,他是什么就有什么的属性
srcElement 是事件初始目标的html元素对象引用,因为事件通过元素容器层次冒泡,可以在任一层进行处理,所以由一个属性指向产生初始事件的元素是很有帮助的.有了元素的引用,就可以读/写改元素的属性,并调用他的任何方法.
用法:
event.srcElement [ = oObject ]
可能的值:
oObject:
Object that specifies 或者 receives the event that fired.
该属性可读/写,没有默认值。IE4中只读。
详细说明和例子:
http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/srcelement.asp
例子1:
<script>
function a()
{
alert("您点击的标记是:" + event.srcElement.tagName) ;
//tagName还 可以是id
}
</script>
<body onclick="a()">
点鼠标测试<br>
<input>
<a href=#>test</a>
<div>测试</div>
<p>测试</p>
<span>测试</span>
例子 2:
通过一个函数实现每个<Td>里添加一个onclick,并对对应相应的ID
<table border=1 width=50%>
<tr>
<td id="d1" align="center">1</td>
<td id="d2" align="center">2</td>
<td id="d3" ALIGN="center">3</td>
<td id="d4" align="center">4</td>
<td id="d5" align="center">5</td>
<td id="d6" ALIGN="center">6</td>
</tr>
</table>
<script>
for(var i=1;i<=6;i++)
{
eval("d"+i).attachEvent("onclick",Fclick);
}
function Fclick()
{
alert(event.srcElement.id);
}
</script>
例子3:
比如:var e=window.event.srcElement;
e.style.backgroundColor='blue';
srcElement表示事件源
在mozilla中
el = e.target ? e.target : e.srcElement;
例二
<html>
<script>
function getEvent()
{var i = 0;
if(document.all)return window.event;
func=getEvent.caller;
while(func!=null)
{
var arg0=func.arguments[0];
if(arg0)
{
if(arg0.constructor==Event){
return arg0;
}
}
func=func.caller;
}
return null;
}
document.onclick = function(e) //要兼容FF的话,这个参数一定要写上
{
e = getEvent();
var srcElement = e.srcElement || e.target;
alert(srcElement.innerHTML);
}
</script>
<body>
window.event事件测试<br/>
<input type="button" name="b_test" value="测试">
</body>
</html>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xiangrong530/archive/2007/12/07/1922530.aspx
用srcElement实现添加效果
<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
<!--
.l {FONT-SIZE:10pt;}
-->
</style>
<script language="javascript">
function addthis(){
var srcE=event.srcElement?event.srcElement:event.target;
if((srcE.tagName=="INPUT")&&(srcE.type.toLowerCase()=="checkbox")){
var thev=srcE.value;
var theid=srcE.id;
//alert(theid);
if(theid.indexOf('xinge')!=-1){
thetag="xinge";
}else{
thetag="likes";
}
//alert(thetag);
switch(thetag){
case "likes":
var s=document.forms[0].likes.value.indexOf(thev);
if(s==-1){
document.forms[0].likes.value+=thev+",";
}
break;
case "xinge":
var s=document.forms[0].xinge.value.indexOf(thev);
if(s==-1){
document.forms[0].xinge.value+=thev+",";
}
break;
}
}}
document.onmousedown=addthis;
</script>
</head>
<body>
<form name="form1"><textarea name="likes" cols="40" rows="8" id="textarea9" style="font-size: 9pt;padding:4px;"></textarea>
<textarea name="xinge" cols="40" rows="8" id="textarea9" style="font-size: 9pt;padding:4px;"></textarea>
<table border="0" width="100%" cellspacing="0" cellpadding="0" >
<tr>
<td colspan="5">兴趣爱好</td>
</tr>
<tr>
<td><input name="activity" type="checkbox" value="电脑/网络" id="activity_1" />
<label for="activity_1" class="l">电脑/网络</label></td>
<td><label for="activity_2" class="l">
<input name="activity2" type="checkbox" value="电子游戏" id="activity_2"/>
电子游戏</label></td>
<td><input name="activity2" type="checkbox" value="体育运动" id="activity_3" />
<label for="activity_3" class="l">体育运动</label></td>
<td><input name="activity2" type="checkbox" value="饮酒" id="activity_4" />
<label for="activity_4" class="l">饮酒</label></td>
<td><input name="activity2" type="checkbox" value="品茗" id="activity_5" />
<label for="activity_5" class="l">品茗</label></td>
</tr>
<tr>
<td><input name="activity2" type="checkbox" value="弹琴" id="activity_6" />
<label for="activity_6" class="l">弹琴</label></td>
<td><input name="activity2" type="checkbox" value="看电影/电视" id="activity_7" />
<label for="activity_7" class="l">看电影/电视</label></td>
<td><input name="activity2" type="checkbox" value="下棋/打牌" id="activity_8" />
<label for="activity_8" class="l">下棋/打牌</label></td>
<td><input name="activity2" type="checkbox" value="观光旅游" id="activity_9" />
<label for="activity_9" class="l">观光旅游</label></td>
<td><input name="activity2" type="checkbox" value="逛街购物" id="activity_10" />
<label for="activity_10" class="l">逛街购物</label></td>
</tr>
<tr>
<td><input name="activity2" type="checkbox" value="阅读" id="activity_11" />
<label for="activity_11" class="l">阅读</label></td>
<td><input name="activity2" type="checkbox" value="写作" id="activity_12" />
<label for="activity_12" class="l">写作</label></td>
<td><input name="activity2" type="checkbox" value="烹调" id="activity_13" />
<label for="activity_13" class="l">烹调</label></td>
<td><input name="activity2" type="checkbox" value="舞会/卡拉OK" id="activity_14" />
<label for="activity_14" class="l">舞会/卡拉OK</label></td>
<td><input name="activity2" type="checkbox" value="健身/武术" id="activity_15" />
<label for="activity_15" class="l">健身/武术</label></td>
</tr>
<tr>
<td colspan="5">性格:</td>
</tr>
<tr>
<td><input name="activity2" type="checkbox" value="活泼" id="xinge_16" />
<label for="xinge_16" class="l">活泼</label></td>
<td><input name="activity2" type="checkbox" value="尖刻" id="xinge_17" />
<label for="xinge_17" class="l">尖刻</label></td>
<td><input name="activity2" type="checkbox" value="自闭" id="xinge_18" />
<label for="xinge_18" class="l">自闭</label></td>
<td><input name="activity2" type="checkbox" value="等等" id="xinge_19" />
<label for="xinge_19" class="l">等等 </label></td>
<td><input name="activity2" type="checkbox" value="外向" id="xinge_20" />
<label for="xinge_20" class="l">外向</label></td>
</tr>
</table>
</form>
</body>
</html>