javascript中动态添加事件!!

  1. 直接写函数:
    <script language="javascript">
    document.getElementById(
    "result").onclick=function(){document.getElementById("result").innerText="动态添加事件";};
    </script>
     
  2. 使用attachEvent
    <script language="javascript">
          
    function aEvent() {
               
    var oBtn = document.all['oBtn'];
               oBtn.attachEvent('onclick', sText);
          }
          
          
    function dEvent() {
               
    var oBtn = document.all['oBtn'];
               oBtn.detachEvent('onclick', sText);           
          }

          
    function sText() {
               window.alert('看到效果了吗
    ^^');
          }
    </script>
    <button id="oBtn">点击我查看效果</button>
    <button onclick="aEvent();">添加事件</button>
    <button onclick="dEvent();">取消事件</button>
posted @ 2006-11-29 09:25  四眼蒙面侠  阅读(3855)  评论(2)    收藏  举报