document.log():"undefined不是函数”;

document.log(): "undefined is not a function"

本文关键字:undefined 函数 quot log document      更新时间:2023-09-26

我知道,我知道,这是一个常见的错误,你应该看看其他人等等。我已经找到了,但我不可能找到解决方案。

这是我的全部代码。

<div id="div"></div>
<script>
function item(id, price, name, icon){
    this.id = id;
    this.initPrice = 0;
    this.price = price;
    this.name = name;
    this.icon = icon;
    document.getElementById("div").innerHTML += this.name + " = $" + price + " <input id='"itemNum" + this.id + "'" onchange='"item" + this.id + ".calc()'" type='"number'" style='"width: 40px;'" value='"1'"></input><br>";
    this.calc = function(){
        document.log("yay");
    }
}
var item1 = new item(1, 700, "Barrel #1", "");
item1.calc();
</script>

我在第14行得到错误,当我改变输入时,我在第十四行再次得到相同的错误。我做错了什么?我该怎么解决这个问题?

您需要使用:

console.log("yay");

而不是:

document.log("yay");

Fiddle演示

尝试console.log()而不是document.log()