【发布时间】:2016-01-29 16:02:26
【问题描述】:
我有如下代码。我制作了名为“Car”的Javascript类,并在构造函数中将事件添加到html“”标签。但是这个事件不能触发。
有人可以帮我吗?
谢谢, A.
function Car(model, brand) {
this.model = model;
this.brand = brand;
document.getElementById('button').addEventListener('onclick', this.info);
}
// method
Car.prototype.info = function() {
alert("It is " + this.model);
};
// define cariable
var car = new Car("RX7", "Mazda");
// Invoke method in html
//car.info();
// second way
window.onload = function() {
var car = new Car("RX7", "Mazda");
};
<button id="button">Click me</button>
【问题讨论】:
标签: javascript html class oop events