event.srcElement
时间:2010-12-25 来源:menghe
我们可以调用他的各种属性 就像:document.getElementById("")这样的功能。
经常有人问 firefox 下的 event.srcElement 怎么用,在此详细说明:
IE下,event对象有srcElement属性,但是没有target属性;Firefox下,event对象有target属性,但是没有srcElement属性.但他们的作用是相当的,即:
firefox 下的 event.target = IE 下的event.srcElement
解决方法:使用obj(obj = event.srcElement ? event.srcElement :event.target;)来代替IE下的event.srcElement或者Firefox下的event.target。
下面用三个例子来讲解一下event.srcElement的含义:
例一:
<div id="div_001">
<formid="form_001">
<inputtype="button" id="button_001_id" name="button_001_Name"value="单击查看"
class="button_001_Class" onclick="Get_srcElement(this)">
</form>
</div>
<script>
function Get_srcElement(obj)
{
var srcElement = "";
srcElement = srcElement + "\n" + "event.srcElement.objectId : " + obj.id;
srcElement = srcElement + "\n" + "event.srcElement.objectTagName : " + obj.tagName;
srcElement = srcElement + "\n" + "event.srcElement.type : " + event.srcElement.type;
srcElement = srcElement + "\n" + "event.srcElement.value : " + event.srcElement.value;
srcElement = srcElement + "\n" + "event.srcElement.name : " + event.srcElement.name;
srcElement = srcElement + "\n" + "event.srcElement.className : " +
event.srcElement.className;
srcElement = srcElement + "\n" + "event.srcElement.parentElement.id : " +
event.srcElement.parentElement.id;
srcElement = srcElement + "\n" + "event.srcElement.getAttribute : " +
event.srcElement.getAttribute;
alert(srcElement);
}
</script>
【注:】从例一可以看出event.srcElement跟传过来的obj对象是一样的。
例二:
<select name="selectname"onchange="alert(event.srcElement.options
[event.srcElement.selectedIndex].value)">
<optionvalue="001">1</option>
<optionvalue="002">2</option>
<optionvalue="003">3</option>
<optionvalue="004">4</option>
<optionvalue="005">5</option>
</select>
【注:】从例二可以看出event.srcElement就是select控件本身,所以把event.srcElement去掉,直接写成
options[selectedIndex].value也是一样的。
例三:
<div onclick = "Get_srcElement()">
<div id = "001" style ="border:1px solid red; width:100%;
<span id="node001">这是第1结点div001<span>
<div id = "002" style = "border:1px solid red;width:80%;
<span id ="node002">这是第2结点div002<span>
<div id = "003" style = "border:1px solid red;width:50%;
<span id ="node003">这是第3结点div003<span>
</div>
<divid = "004" style = "border:1px solid red; width:50%;
<span id ="node003">这是第3结点div004<span>
</div>
</div>
</div>
<script>
function Get_srcElement()
{
var srcElement = "";
srcElement = srcElement + "\n" + "event.srcElement.children[0].tagName: " +
event.srcElement.children[0].tagName;
srcElement = srcElement + "\n" + "event.srcElement.firstChild.id : " +
event.srcElement.firstChild.id;
srcElement = srcElement + "\n" + "event.srcElement.lastChild.id : " +
event.srcElement.lastChild.id;
alert(srcElement);
}
</script>
</div>
【注:】这个例子对理解event.srcElement是最有利的,例子中一共有3层DIV,鼠标点击哪层DIV,