XML DOM target 事件属性
XML基础 2023-08-13 19:20:18小码哥的IT人生shichen
XML DOM target 事件属性
定义和用法
target 事件属性可返回事件的目标节点(触发该事件的节点),如生成事件的元素、文档或窗口。
语法
event.target
实例
下面的例子可获得触发事件的元素:
<html>
<head>
<script type="text/javascript">
function getEventTrigger(event)
{
x=event.target
;
alert("The id of the triggered element: "
+ x.id);
}
</script>
</head>
<body >
<p id="p1" onmousedown="getEventTrigger(event)">
Click on this paragraph. An alert box will
show which element triggered the event.</p>
</body>
</html>
TIY
完整实例【target event】:
<html>
<head>
<script type="text/javascript">
function getEventTrigger(event)
{
x=event.target;
alert("The id of the triggered element: " + x.id);
}
</script>
</head>
<body >
<p id="p1" onmousedown="getEventTrigger(event)">
Click on this paragraph. An alert box will show which element triggered the event.</p>
</body>
</html>
可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html
获得触发事件的元素(IE 浏览器不支持)。