我对JavaScript很新,我试图用一个叫做“按钮”的按键来设置一个叫做“数字”的原型,该按键用你通过它的任何ID来获取HTML元素。 HTML中存在一个ID为“1”的元素。从对象原型中使用文档方法?
我想知道为什么这不起作用?
function number(num, idOfNum) {
this.val = num;
this.button = document.getElementById(idOfNum);
};
var one = new number(1, "one");
console.log(one.button); // null?
尝试将其放在主体的末尾或将其包装在window.onload = function(){} – baao