我需要从thediv.onclick
内部调用this.load
函数的帮助。我删除了大部分代码,所以它非常基本,但我真的找不到一种方法来实现它。这里是我目前有:Javascript,通过对象构造函数从另一个onclick调用函数
function CreatePizza(Name, Toppings) {
this.n = Name;
this.t = Toppings;
this.load = function loadpizza() {
//function that i want to be called
}
this.create = function button() {
var thediv = document.createElement("div");
thediv.onclick = function() {
// Call this.load function here
}
}
}
,如果你只需要调用'this.load()''的功能thediv.onclick'里面会发生什么? –
@MatthewHerbst因为函数获取它自己的上下文,所以'this'的值将会改变。只需将其分配给一个变量并从该变量中调用它。 – somethinghere
@somethinghere我知道 - 我想让OP向我们展示他正在试图让他可以学习:) –