不知JavaScript中有没有函数可以返回鼠标点击位置的标签节点?有没有函数可以返回当前鼠标所在位置的标签节点?麻烦指点一下,谢谢!

解决方案 »

  1.   

    IE<script type="text/javascript">
    function doit(){
    alert(event.srcElement.nodeName)
    }
    window.onload=function(){
        document.onclick=doit;
    }
    </script>
    </head><body>
    <div>
    <div>xxxx</div>
    </div>
    </body>
    </html>
      

  2.   


    <html>
    <script type="text/javascript">
    function doit(evt){
    evt = window.event?window.event:evt;
        var node = window.event?evt.srcElement:evt.target;
        alert(node.nodeName);
    while(node.nodeName.toUpperCase()!="HTML"){
           node = node.parentNode;
           alert(node.nodeName);
        }
    }
    window.onload=function(){
        if(document.all) document.attachEvent("onclick",function(){doit(event);});
        else document.addEventListener("click",function(evt){doit(evt);},false);
    }
    </script>
    </head><body>
    <div>
    <div>xxxx</div>
    </div>
    </body>
    </html>
      

  3.   

    在页空白区域名点击会提示是html元素触发的。这个要看怎么理解。
    如果你将事件加在body元素上,那么就会提示是body元素触发的。
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
        <script type="text/javascript">        window.onload = function()
            {
                document.documentElement.onclick = function()
                {
                    var oEvent = arguments[0] || window.event;
                    var oType = oEvent.type;
                    if (oType == "click")
                    {
                        var oElem = oEvent.target || oEvent.srcElement;
                        window.alert(oElem.nodeName)
                    }
                }
            }
        
        </script>
    </head>
    <body style="margin:0px;">
    <div id="container">
    fffffff
    </div>
    <input type="button" value="test" />
    </body>
    </html>
      

  4.   

    我觉得比较笨的办法哈,在每个标签内写上onclick='getSrc(this)'
    然后再js里面写
    function getSrc(obj) {
      return obj;
    }
    不知道这样行不?