简体   繁体   English

错误:未捕获的类型错误:未定义不是函数

[英]Error: Uncaught TypeError: undefined is not a function

I am trying to make a js game though when I try and trigger a function using onclick nothing happens and then when I went onto console it came up with this error message everytime I clicked it:我正在尝试制作一个 js 游戏,但是当我尝试使用onclick触发一个函数时,什么也没有发生,然后当我进入控制台时,每次单击它时都会出现此错误消息:

Uncaught TypeError: undefined is not a function未捕获的类型错误:未定义不是函数

This is how I define my function:这就是我定义我的函数的方式:

var myfunction = function () {

I know that it is nothing to do with the function though as I call it after I make it and it works fine.我知道这与函数无关,尽管我在创建它之后调用它并且它工作正常。

I have also checked whether it is the click by putting an alert in the onclick though that worked fine.我还通过在 onclick 中放置警报来检查它是否是点击,尽管效果很好。

I know this is a common problem, though have looked at others and found nothing.我知道这是一个常见的问题,尽管看了其他人并没有发现任何问题。

http://jsfiddle.net/Hive7/DQnmF/ http://jsfiddle.net/Hive7/DQnmF/

JavaScript: JavaScript:

var attack = function () {
    while (slaying === true) {
        if (youHit !== 1) {
            totalDamage = totalDamage += damageThisRound;
            document.getElementById('hit').style.display = 'inline';  
            //alert('You hit!!! ' + totalDamage * 10 + ' damage');
            health = health - totalDamage * 10;
            setTimeout(function(){document.getElementById('health').style.width = health + 'px'}, 400);
            setTimeout(function(){document.getElementById('hit').style.display = 'none'}, 500);
            if (totalDamage >= 4) {
                //alert('the dragons dead');
                slaying = false;
            }
        } else {
            document.getElementById('miss').style.display = 'inline'; 
            setTimeout(function(){document.getElementById('miss').style.display = 'none'}, 500);
            //alert('You missed');
        }
        slaying = false;
    }
}

document.getElementById('character').style.color = '#ccc';
document.getElementById('enemy').style.position = 'absolute';
document.getElementById('enemy').style.right = '0';
document.getElementById('game').style.position = 'relative';
document.getElementById('game').style.width = '1004px';
document.getElementById('characters').style.position = 'relative';
document.getElementById('health-container').style.right = '0';
document.getElementById('health-container').style.position = 'absolute';
document.getElementById('health-container').style.bottom = '30px';
document.getElementById('health-container').style.width = '200px';
document.getElementById('health-container').style.height = '41px';
document.getElementById('health').style.backgroundColor = 'red';
document.getElementById('health').style.height = '41px';
document.getElementById('health').style.width = '200px';
document.getElementById('miss').style.position = 'absolute';
document.getElementById('miss').style.right = '20px';
document.getElementById('miss').style.bottom = '120px';
document.getElementById('miss').style.display = 'none';
document.getElementById('hit').style.position = 'absolute';
document.getElementById('hit').style.right = '20px';
document.getElementById('hit').style.bottom = '120px';
document.getElementById('hit').style.display = 'none';
var slaying = true;
var youHit = Math.floor(Math.random() * 6 + 1);
var totalDamage = 0;
var damageThisRound = Math.floor(Math.random() * 4 + 1);
var num = document.getElementById('health').style.width.length - 2;
var health = document.getElementById('health').style.width;
health = health.substring(0, num);
attack();

I found the answer in the end.我最终找到了答案。 I used this instead of using the onclick method:我使用它而不是使用 onclick 方法:

document.getElementById('attack').addEventListener('click', function() {
    myfunction();
}, false);

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

相关问题 函数错误:未捕获TypeError:未定义不是函数 - Function error: Uncaught TypeError: undefined is not a function 错误消息“Uncaught TypeError: undefined is not a function”(数据表) - Error message "Uncaught TypeError: undefined is not a function" (DataTable) Firebase上的“未捕获的TypeError:未定义不是函数”错误 - “Uncaught TypeError: undefined is not a function” error on firebase 为什么会出现错误“未捕获的TypeError:未定义不是函数”? - Why is there an error, “Uncaught TypeError: undefined is not a function”? 为什么会出现错误“未捕获的TypeError:未定义不是函数”? - Why is there an error, “Uncaught TypeError: undefined is not a function”? dataTable引发错误未捕获的TypeError:未定义不是函数 - dataTable raise error Uncaught TypeError: undefined is not a function 模态错误-未捕获的TypeError:undefined不是函数模态 - Modal error - Uncaught TypeError: undefined is not a function modal browserify错误“未捕获的TypeError:未定义不是函数” - browserify error 'Uncaught TypeError: undefined is not a function ' Boostrap Popover错误:未捕获TypeError:undefined不是函数 - Boostrap Popover error: Uncaught TypeError: undefined is not a function 经典的jQuery错误-Uncaught TypeError:undefined不是一个函数 - A classical jQuery error - Uncaught TypeError: undefined is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM