在元素上添加事件监听器 - Javascript

sim*_*ed. 13 javascript dom element event-listener

有没有办法让我从onload函数中搜索javascript中的onblur或onclick事件?而不是在元素本身.

<input type="button" id="buttonid" value="click" onclick="func()">
Run Code Online (Sandbox Code Playgroud)

成为类似的东西

function onload() {
      var button = document.getElementById("buttonid");
      button.addEventListener("onclick", function() { alert("alert");});
}
Run Code Online (Sandbox Code Playgroud)

编辑

<html>

<head>

     <script>

     function onload() {

        var button = document.getElementById("buttonid");

        if(button.addEventListener){
             button.addEventListener("click", function() { alert("alert");});
        } else {
             button.attachEvent("click", function() { alert("alert");});
        };
     };

          window.onload = onload;


     </script>

</head>

<body>

<input type="button" id="buttonid" value="click">


</body>

</html>
Run Code Online (Sandbox Code Playgroud)

UPDATE

 <script type="text/javascript">

 function on_load() {

    var button = document.getElementById("buttonid");

    if(button.addEventListener){
         button.addEventListener("click", function() { alert("alert");});
    } else {
         button.attachEvent("click", function() { alert("alert");});
    };
 };

      window.onload = on_load();


 </script>
Run Code Online (Sandbox Code Playgroud)

She*_*hef 17

你这样做的方式很好,但你的事件监听器click应该是这样的:

button.addEventListener("click", function() { alert("alert");});
Run Code Online (Sandbox Code Playgroud)

请注意,click事件应该附上"click",而不是"onclick".

您也可以尝试以旧方式执行此操作:

function onload() {
   var button = document.getElementById("buttonid");
   // add onclick event 
   button.onclick = function() { 
        alert("alert");
   }
}
Run Code Online (Sandbox Code Playgroud)

更新1

您还需要监视IE <9,因为那些V使用attachEvent().像这样附加事件,因此它可以与恐龙浏览器一起使用:

if(button.addEventListener){
    button.addEventListener('click', function() { alert("alert");});    
} else if(button.attachEvent){ // IE < 9 :(
    button.attachEvent('onclick', function() { alert("alert");});
}
Run Code Online (Sandbox Code Playgroud)

更新2

根据您的编辑,这应该工作 得很好.

<html>
    <head>
        <script type="text/javascript">
            function init() {
                var button = document.getElementById("buttonid");
                if(button.addEventListener){
                    button.addEventListener("click", function() { alert("alert");}, false);
                } else if(button.attachEvent){
                    button.attachEvent("onclick", function() { alert("alert");});
                }
            };
            if(window.addEventListener){
                window.addEventListener("load", init, false);
            } else if(window.attachEvent){
                window.attachEvent("onload", init);
            } else{
               document.addEventListener("load", init, false);
            }
        </script>
    </head>
    <body>
        <input type="button" id="buttonid" value="click">
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

请不要使用window.onload = on_load();,这将阻止所有其他onload事件侦听器被触发,或者您冒着被覆盖的事件监听器的风险.考虑onload按照我上面建议的方式附加事件.