javascript当中绑定事件和方法

8.绑定事件和方法

马克-to-win:once, long time to know that "script" must be put in behind, while "input" must be put in front,
  otherwise document.getElementById("button1"); can not find the "button1",alert("button1 is "+ button1); 结果就是null,为什么这次跟往常我们的印象不一样了呢?因为往常我们先写一段script,之后在body中写上诸如<INPUT TYPE="button" οnclick="abc",之类的话,这样上面的abc这样的代码开始不会被执行,里面的诸如document.getElementById ("button1");也就正确了。这里为什么跟往常不一样呢?因为要在一开始时,先给button绑上事件代码,否则button无事件响应。

例 8.1(commonEventPrerequisiteIEFF.html)

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<INPUT TYPE="button" NAME="button1" ID="button1" VALUE="单击我"/>
<script>
    function testie()
    {
        alert("单击按钮ie");
    }
    function testFF()
    {
        alert("单击按钮FireFox");
    }
 
        /**/
        /*obj.addEventListener("click",function(){func("看到我了吧");},false);
  the third argument is: A Boolean flag value that indicates whether the event listener should
use bubbling (由里向外) or capture (由外向里) event propagation. 只要我们
知道第三个参数和事件冒泡有关就可以了。缺省值为假,即冒泡的意思。具体例子参考后面的事件冒泡例子。
     */
        var button1 = window.document.getElementById("button1");
        alert("button1 is "+ button1);
        alert("document.all is" + document.all);
        alert(typeof(window.addEventListener) + " is typeof window.addEventListener");
        alert(typeof(window.attachEvent) + " is typeof window.attachEvent");
        alert(window.addEventListener + " is window.addEventListener");
        alert(window.attachEvent + " is window.attachEvent");
        if (typeof window.attachEvent === "object")
        {
            alert("ie");
            button1.attachEvent("onclick", testie);
        }
        if (typeof window.addEventListener === "function")
        {
            alert("firefox");
            button1.addEventListener("click", testFF, false);
        }
       // button1.addEventListener("click",test,false);
        //button1.attachEvent("onclick" , test);
        var str = "";

</script>
 

更多请见:https://blog.csdn.net/qq_43650923/article/details/102210095

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值