程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> eventlistener-javascript中使用事件委托無法獲取到event.target

eventlistener-javascript中使用事件委托無法獲取到event.target

編輯:編程綜合問答
javascript中使用事件委托無法獲取到event.target
<!doctype html>
<html>
<head>
    <title>image's transition</title>
    <script type="text/javascript">
    window.onload = function(){
        var pic_wrapper = document.getElementById("pic_wrapper");
        var ul = document.getElementsByTagName("ul")[0];
        var lis= document.getElementsByTagName("li");
        var imgs = pic_wrapper.getElementsByTagName("img");
                    //使用for循環綁定事件處理函數,通過測試
        // for (var i = 0; i < lis.length; i++) {
        //  var img = imgs[i];
        //  var li = lis[i];
        //  setOpacity(li,img);

        //  function setOpacity(li1,img1){
        //      li1.onmouseover = function(){
        //          setAllOpacity();
        //          img1.style.opacity = "1";
        //      };
        //  }
        // };
        function setAllOpacity(){
            for(var i = 0; i < imgs.length; i++){
                imgs[i].style.opacity = "0";
            }
        }
        //使用事件委托方式,但存在問題
        ul.onmouseover = function(event){
            event = event || window.event;
            var target = event.target || event.srcElement;
            switch(target.id){//問題在於target.id,通過正常交互無法獲取target.id
                case "li1":
                    setAllOpacity();
                    imgs[0].style.opacity = "1";alert("li1");
                    break;
                case "li2":
                    setAllOpacity();
                    imgs[1].style.opacity = "1";alert("li2");
                    break;
                case "li3":
                    setAllOpacity();
                    imgs[2].style.opacity = "1";
                    break;
                case "li4":
                    setAllOpacity();
                    imgs[3].style.opacity = "1";
                    break;
            }
        };

        function fire(elem, type){  
            var evt;  
            if(document.createEventObject){ 
                elem.fireEvent('on'+type);  
            }else{
                evt = document.createEvent('MouseEvents');   
                evt.initEvent(type, true, false);  
                elem.dispatchEvent(evt);  
            }  
        }  
        fire(lis[0],"mouseover");//通過手動觸發事件,可以獲取target
    };
    </script>
    <style type="text/css">
    *{
        padding: 0;
        border: 0;
        margin: 0;
    }
    #wrapper{
        width: 800px;
        height: 600px;
        margin: 20px auto;
        position: relative;
        overflow: hidden;
    }
    #pic_wrapper{
        position: absolute;
    }
    #pic_wrapper img{
        width: 800px;
        height: 600px;
        position: absolute;
        transition: opacity 0.5s ease-in;
        opacity: 0;
    }
    .num{
        position: absolute;
        bottom: 50px;
        left: 390px;
    }
    li{
        display: inline;
        cursor: pointer;
    }
    </style>

</head>
<body>
    <div id="wrapper">
        <div id="pic_wrapper">
            <img src="transition1.jpg">
            <img src="transition2.jpg">
            <img src="transition3.jpg">
            <img src="transition4.jpg">
        </div>
        <ul class="num">
            <li id="li1"><img src="num.jpg"></li>
            <li id="li2"><img src="num.jpg"></li>
            <li id="li3"><img src="num.jpg"></li>
            <li id="li4"><img src="num.jpg"></li>
        </ul>
    </div>
</body>
</html>

最佳回答:


switch (**target.id||target.parentNode.id**) {

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved