javascript中动态添加事件!!
- 直接写函数:
    <script language="javascript">
 document.getElementById("result").onclick=function(){document.getElementById("result").innerText="动态添加事件";};
 </script>
- 使用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>
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号