Attaching event handler to DOM elements

前端 未结 6 727
[愿得一人]
[愿得一人] 2021-01-28 12:30

I am working on a tic tac toe game, which is almost complete. The only thing I am left wondering about is if it is possible to add an event handler for onclick fro

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-28 12:40

    Your answer is in following 5 lines. Try it :) If someone copies/converts my code in new post or my post to j-query, That is welcome with no problem.

    var yourDivID = document.getElementById('your_Div_ID');
    yourDivID.addEventListener('click', function (){  playerMove(this); }, false);
    function playerMove(divElement)
    {
        alert(divElement.id);
    }
    

    I have tried to put complete demo Here on jsfiddle.net, You can click any div out of nine to check its event if link does not work, then you can check the following code (working for me WIN7 and FireFox)

    
    
     Add Events Dynamically 
    
        
    
            
            

提交回复
热议问题