我正在尝试制作javascript应用程序,用户可以在其中输入他/她想要制作的cookie类型,然后选择他们想要的cookie数量。一旦用户进行此选择并按下“烘焙”按钮,cookie就会随机出现在页面中。我有一个任务,我必须在页面上生成随机div,这有点给了我这个想法,这只是为了娱乐和练习。
我的问题是,我希望用户能够通过点击它们获得有关他/她创建的cookie的独特信息,而且我无法使其工作。
答案 0 :(得分:0)
(1) 单击事件未正确定义:
aCookie.onlclick = cookie.display();
// typing error onlclick
// cookie.display() actually it call display function, doesn't give reference
更改为:
aCookie.onclick = cookie.display;
(2) 设置id" kind"输入框
(3) 当前代码集属性(id,kind,x,y)到Cookie的实例,而不是元素。 更改代码,以便元素是参数&设置属性。
//pass element to set properties
function Cookie(elem,id, kind, x, y) {
elem.id = id;
elem.kind = kind;
elem.x = x;
elem.y = y;
this.display = function () {
alert("Cookie number: " + this.id + "; is a: " + this.kind + "; cookie : " +
" and it is situated on coordinates ; " + this.y + " and " + this.x + " on the cookie pan");
}
}