js获取鼠标所在html元素的id和属性:
<div onclick="Get_srcElement()"> <div id="001" style="border:1px solid red;width:100%;height:500px"> <span id="node001">这是第1结点div001<span> <div id="002" style="border:1px solid red;width:80%;height:300px"> <span id="node002">这是第2结点div002<span> <div id="003" style="border:1px solid red;width:50%;height:200px"> <span id="node003">这是第3结点div003<span> </div> <div id="004" style="border:1px solid red;width:50%;height:200px"> <span id="node003">这是第3结点div004<span> </div> </div> </div> <script> function Get_srcElement() { var srcElement="" srcElement= srcElement + " \n" + "event.srcElement.id : " + event.srcElement.id 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> <div id="div_001"> <form id="form_001"> <input type="button" id="button_001_id" name="button_001_Name" value="单击查看" class="button_001_Class" onclick="Get_srcElement2(this)"> </form> </div> <script> function Get_srcElement2() { var srcElement="" srcElement= srcElement + " \n" + "event.srcElement.id : " + event.srcElement.id srcElement= srcElement + " \n" + "event.srcElement.tagName : " + event.srcElement.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>
目前不兼容火狐,参考下面代码进行修改看看可行否,我有时间再修改本篇日志。
function Get_srcElement(e)
{
ee=window.event||e;
var tmpObj = ee.srcElement || ee.target;
var srcElement = srcElement + " " + "event.srcElement.id : " + tmpObj .id
alert(srcElement)
}