jquery参数传递问题

光光-Leo
领域专家: 后端开发技术领域
2014-07-23 07:45:22
_hover: function(id) {
var self = this,
_config = self.config,
_cache = self.cache;
this.id=id;
//鼠标悬停
$(_config.list + ' li').hover(function(){
/* 1 * /
alert(id);
}
我现在就是想把最上边的那个参数id传到1所在的那个function里,但实现不了,这样一打印就像是 undifine,怎么破??
...全文
211 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
XZowie 2014-07-24
  • 打赏
  • 举报
回复
加一個字就好
_hover: function(id) {
    var self = this,
        _config = self.config,
        _cache = self.cache;
    this.id=id;
    //鼠标悬停
    $(_config.list + ' li').hover(function(){
        /*    1    * /
        alert(self.id);
    });
}
光光-Leo 2014-07-24
  • 打赏
  • 举报
回复
引用 2 楼 u012116457 的回复:
[quote=引用 1 楼 jikeytang 的回复:] 需要在调用的时候传入: o._hover(1);

var o = {
    id : 1,
    _hover: function(id) {
        var self = this,
            _config = self.config,
            _cache = self.cache;

        this.id=id;
        alert(id);
        //鼠标悬停
        $(_config.list + ' li').hover(function(){
            //    /*    1    * /
            alert(id);
        });
    }
}
o._hover(1);
我也这样写了, 代码多贴点: Microblog.prototype = { _hover: function(id) { var self = this, _config = self.config, _cache = self.cache; this.id=id; //鼠标悬停 $(_config.list + ' li').hover(function(){ alert(id); } } 调用的时候: function sayClick(id){ Microblog.prototype._hover(id); }; 我这个可以传到_hover()里,但是传不到里边那个函数里 [/quote] 确实按你的方法改好了,多谢指教。
豪情 2014-07-23
  • 打赏
  • 举报
回复
prototype是原型,调用的时候需要初始化。这是由js的语言规则决定的。 demo here

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>test</title>
    <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.10.2/jquery-1.10.2.min.js"></script>
</head>
<body>
<input id="btn" type="submit" value="test" onclick="sayClick(this.id)" />
<script type="text/javascript">
    function Microblog(){ }
    Microblog.prototype = {
        _hover: function(id) {
            var self = this,
                    _config = self.config,
                    _cache = self.cache;
            this.id=id;
            alert(id);
            //鼠标悬停
            $(_config.list + ' li').hover(function(){
                alert(id);
            });
        }
    }
    function sayClick(id){
        var m = new Microblog();
        m._hover(id);
    };
</script>
</body>
</html>
光光-Leo 2014-07-23
  • 打赏
  • 举报
回复
引用 1 楼 jikeytang 的回复:
需要在调用的时候传入: o._hover(1);

var o = {
    id : 1,
    _hover: function(id) {
        var self = this,
            _config = self.config,
            _cache = self.cache;

        this.id=id;
        alert(id);
        //鼠标悬停
        $(_config.list + ' li').hover(function(){
            //    /*    1    * /
            alert(id);
        });
    }
}
o._hover(1);
我也这样写了, 代码多贴点: Microblog.prototype = { _hover: function(id) { var self = this, _config = self.config, _cache = self.cache; this.id=id; //鼠标悬停 $(_config.list + ' li').hover(function(){ alert(id); } } 调用的时候: function sayClick(id){ Microblog.prototype._hover(id); }; 我这个可以传到_hover()里,但是传不到里边那个函数里
豪情 2014-07-23
  • 打赏
  • 举报
回复
需要在调用的时候传入: o._hover(1);

var o = {
    id : 1,
    _hover: function(id) {
        var self = this,
            _config = self.config,
            _cache = self.cache;

        this.id=id;
        alert(id);
        //鼠标悬停
        $(_config.list + ' li').hover(function(){
            //    /*    1    * /
            alert(id);
        });
    }
}
o._hover(1);

87,984

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧