js獲取鼠標所在html元素的id和屬性:html
- <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>
目前不兼容火狐,參考下面代碼進行修改看看可行否,我有時間再修改本篇日誌。node
function Get_srcElement(e)
{
ee=window.event||e;
var tmpObj = ee.srcElement || ee.target;
var srcElement = srcElement + " " + "event.srcElement.id : " + tmpObj .id
alert(srcElement)
}web