在我的对象的构造函数中,我创建了一些span标记,我需要将它们引用到同一对象的方法中.
这是我的代码示例:
$(document).ready(function(){
var slider = new myObject("name");
});
function myObject(data){
this.name = data;
//Add a span tag, and the onclick must refer to the object's method
$("body").append("<span>Test</span>");
$("span").click(function(){
myMethod(); //I want to exec the method of the current object
});
this.myMethod = myMethod;
function myMethod(){
alert(this.name); //This show undefined
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码调用该方法,但它不是对象的引用(this.name show undefined)如何解决该问题?
非常感谢!