function Person(id, name, age)
{
this.id = id;
this.name = name;
this.age = age; $("#txt1").click(function() //txt1是一个文本框,点击时显示人的信息
{
$(this).val(id + "/" + name + "/" + age);
//ShowInfo(); //怎么调用Person的方法?
});
}Person.prototype.ShowInfo = function()
{
alert("id: " + this.id + "\r\nname:" + this.name + "\r\nage:" + this.age);
}$(function()
{
var p = new Person("123", "zhang", 20);
p.ShowInfo();
});在文本框的click事件中调用当前对象Person的自定义方法,怎么做(不考虑类及调用的合理性,只为说明问题)?
{
this.id = id;
this.name = name;
this.age = age; $("#txt1").click(function() //txt1是一个文本框,点击时显示人的信息
{
$(this).val(id + "/" + name + "/" + age);
//ShowInfo(); //怎么调用Person的方法?
});
}Person.prototype.ShowInfo = function()
{
alert("id: " + this.id + "\r\nname:" + this.name + "\r\nage:" + this.age);
}$(function()
{
var p = new Person("123", "zhang", 20);
p.ShowInfo();
});在文本框的click事件中调用当前对象Person的自定义方法,怎么做(不考虑类及调用的合理性,只为说明问题)?
解决方案 »
- 用jquery写个人物关系网
- 求助:关于from表单滚动条的问题
- 求 。W3C总是报JavaScript里的尖括号"<>"有问题。。
- 本人想在A.asp页面包含B.asp页面
- 50分求教一个关于initialize,foldersTree的问题
- 【表单提交问题】提交表单后,弹出一对话框,有两个按钮:确定,取消。点击确定后,将表单提交;点击取消,则返回该页面。请问如何实现?
- 你回答我给分
- 征求几个windows media player的函数~~~~~~~
- 我的问题!!!
- 右键菜单识别
- 请问用javascript怎样获取一个datalist控件中的checkbox控件?
- 关于getElementById和getElementByName的问题
id: undefined
name:undefined
age:undefined
$(this).val(id ...); 不能用$(this).val(this.id ...);
p.ShowInfo();既然构造中需要参数,那么你必须传递参数
function Person(id, name, age) {
this.id = id;
this.name = name;
this.age = age;
var _this = this;//保存this
$("#txt1").click(function ()
{
$(this).val(id + "/" + name + "/" + age);
_this.ShowInfo()//调用_this
});
}
要内部函数中使用当前对象的方法 因为变量的作用域问题 要把this提出 当做一个小的全局变量使用