function Player()
{
this.width = 0;
this.height = 0;
};Player.prototype.show = function(w,h)
{
this.width = w;
this.height = h;
alert("width="+ this.width +";height="+ this.height);
};Player.prototype.check = function()
{
$("img").click(function(){
this.show(this.width,this.height);
//在这里要如何调用 show 这个方法,用以上方法总是出错
});
};var player = new Player();
player.check();
{
this.width = 0;
this.height = 0;
};Player.prototype.show = function(w,h)
{
this.width = w;
this.height = h;
alert("width="+ this.width +";height="+ this.height);
};Player.prototype.check = function()
{
$("img").click(function(){
this.show(this.width,this.height);
//在这里要如何调用 show 这个方法,用以上方法总是出错
});
};var player = new Player();
player.check();
解决方案 »
- 大家来说说闭包吧,闭包的好处有哪些,坏处有哪些为什么要用闭包
- mouseover 问题
- setTimeout 的问题 帮忙看看
- javascript if问题
- *************************************对象{}不可以在prototype上定义东西?????
- 浏览器区分cookie
- 用window.open打开一个本地文件时,提示拒绝访问!怎么解决?
- 怎么判断一组radio中被checked的那个的值??
- 一个网页,点击最小化的时候不要放在任务栏,而是像QQ软件那样直接放入系统托盘那里!同志们怎么实现呢?
- 如何获得鼠标在页面中的坐标?
- 求js正则表达式
- 在JS中怎么判断一个程序是否正在运行
this.show(this.width,this.height);
用$(this).show($(this).width,$(this).height);试试看
{
var me = this;
$("img").click(function(){
me.show(me.width,me.height);
});
};
要注意this指向的对象
$(function(){
function Player(){
this.width = 0;
this.height = 0;
};
var player = new Player();
Player.prototype.show = function(w,h){
alert("ssssss"+w)
this.width = w;
this.height = h;
alert("width="+ this.width +";height="+ this.height);
};Player.prototype.check = function(){
// alert("ss")
$("img").click(function(){
//alert(this.width)
player.show(this.width,this.height);
//在这里只能用player调用 show 这个方法,用以上方法总是出错
});
};
player.check();//这个调用了check()函数
});
Player.prototype.check = function(){
// alert("ss")
var temp = this;
$("img").click(function(){
temp.show(this.width,this.height);
//在这里要如何调用 show 这个方法,用以上方法总是出错
});
};这样也行
此句的 this 指$("img") 而不是Player对象