srcElement  是事件初始目标的html元素对象引用,因为事件通过元素容器层次冒泡,可以在任一层进行处理,所以由一个属性指向产生初始事件的元素是很有帮助的.有了元素的引用,就可以读/写改元素的属性,并调用他的任何方法.
<HTML>
<HEAD>
<TITLE>srcElement Property</TITLE>
<STYLE TYPE="text/css">
.bold {font-weight:bold}
.ital {font-style:italic}
</STYLE>
<SCRIPT LANGUAGE="JavaScript">
function highlight() {
var elem = event.srcElement
if (elem.className == "bold") {
document.styleSheets[0].rules[0].style.color = "red"
} else {
elem.style.color = "#FFCC00"
}
}
function restore() {
var elem = event.srcElement
if (elem.className == "bold") {
document.styleSheets[0].rules[0].style.color = ""
} else {
elem.style.color = ""
}
}
</SCRIPT>
</HEAD>
<BODY onMouseDown="highlight()" onMouseUp="restore()">
<H1>srcElement Property</H1>
<HR>
<P>One event handler...</P>
<UL>
<LI>Can
<LI>Cover
<LI>Many
<LI>Objects
</UL>
<P>
Lorem ipsum dolor sit amet, consectetaur adipisicing elit, 
<SPAN CLASS="bold">sed do </SPAN>eiusmod tempor incididunt 
<SPAN CLASS="ital">ut labore et </SPAN>dolore magna aliqua. 
Ut enim adminim veniam, <SPAN CLASS="bold">quis nostrud 
exercitation </SPAN>ullamco laboris nisi ut aliquip ex ea 
<SPAN CLASS="bold">commodo consequat</SPAN>.
</P>
</BODY>
</HTML>

解决方案 »

  1.   

    event.srcElement指向触发事件的node元素
    用于确定事件源
      

  2.   

    srcElement Attribute | srcElement Property--------------------------------------------------------------------------------Retrieves the object that fired the event. SyntaxHTML < srcElement = oObject ... >  
    Scripting event.srcElement [ = oObject ] Possible ValuesoObject Object爐hat specifies the event that fired. The property is read/write. The property has no default value.Expressions can be used in place of the preceding value(s), as of Microsoft?Internet Explorer 5. For more information, see Dynamic Properties.