简体   繁体   English

firefox,未调用javascript函数

[英]firefox , javascript function not being called

i am new at html5 and javascript. 我是html5和javascript的新手。 i have written a basic code for canvas and i have button on html page ,its simple alert. 我已经为canvas编写了基本代码,并且在html页面上有按钮,它的警告很简单。 issue is that its working fine(alert box poping out) for IE-9 , OPERA , chrome but for firefox nothing happens. 问题是,对于IE-9,OPERA,chrome,它的工作正常(弹出警告框),但对于Firefox没有任何反应。 kindly point out where i am committing a mistake. 请指出我犯了一个错误。 here is my code 这是我的代码

  <script> 

       if(window.addEventListener) {

       window.addEventListener('load', function ()
{

           var my = new Array();
           var i=0;
            var canvas = document.getElementById("myCanvas");
            var context = canvas.getContext("2d");
            var imageObj = new Image();
            var imageObj1 = new Image();
            imageObj.onload = function(){
       imageObj1.src = "Sunflower.gif";
       context.drawImage(imageObj, 0, 0);
            }
            imageObj.src = "Crystal-outline.gif";

            change.addEventListener('mousedown', function () {ButtonDown (change)}, false);

            function ButtonDown (change)
{ alert('welcome to my page');}



         init();
}, false); }
    </script> 
</head> 
<body >

    <canvas id="myCanvas" width="870" height="150"> 
    </canvas><br>
    <button id="change" type="button">Change</button>

</body> 

thanks in advance. 提前致谢。

In the following line you're not getting the change element by id but instead trying to call the addEventListener method on it directly where as there is no such object in js. 在下一行中,您不是通过id获取change元素,而是尝试直接在其上调用addEventListener方法,因为js中没有此类对象。 Other browsers allow you to do this for the sake of running badly written scripts. 其他浏览器允许您执行此操作,以运行编写错误的脚本。

change.addEventListener('mousedown', function () {ButtonDown (change)}, false);

it should be changed to 它应该更改为

document.getElementById('change').addEventListener('mousedown', function () {ButtonDown (this)}, false);

Also you need to pass this as the method parameter 另外,您需要this作为方法参数传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM