php - javascript中的这个this代表的是什么呀?
伊谢尔伦
伊谢尔伦 2017-04-11 12:21:15
[JavaScript讨论组]

在一个回调函数中调用了Base.progress.init()的方法,如下所示:

function update_status(data, textStatus) {
    if (textStatus=="success") {
        for(i=0; i< data.length; i++) {
            $("#on"+data[i].uuid).each(function() {
                if (data[i].status=="ON") {
                    $(this).attr('status', "ONLINE");
                } else if (data[i].status=="OFF") {
                    $(this).attr('status', "OFFLINE");
                } else if (data[i].status == "ERRTKN") {
                    $(this).attr('status', "ERRTKN");
                }else {
                    $(this).attr('status', "unknown");
                }
                Base.progress.init();
            }); 

这里的这个Base.progress.init()方法是个javascript方法,如下所示:

Base.progress = {
    init : function(){
        var _this = this;
        $('.progress').each(function(){
            var status = $(this).attr('status');
            if(status.length == 0){_this.unknown(this); return;};
            _this[status](this);
        });
        $('.discinfo').each(function(){
            var status = $(this).attr('status');
            if(status.length == 0){_this.unknown(this); return;};
            _this[status](this);
        });
        $('.progress_ha').each(function(){
            var status = $(this).attr('status');
            if(status.length == 0){_this.unknown(this); return;};
            _this[status](this);
        })
    },

①请问在下面这个JavaScript方法中this和$(this)分别表示的什么呀?
②_thisstatus是什么意思?
③_this.unkown(this)是什么意思?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(3)
PHP中文网

var _this = this

//这个this指的是调用这个方法的东西保存为 _this

//方便在函数里面调用$(this)就是获取$('.progress') $('.discinfo') $('.progress_ha')

// status 因为循环后这个就是个数组了,所以用_this[status]包裹代表调用的这个元素的status属性

PHP中文网

this是对象自己,$(this)是转为jquery对象

阿神

this是你操作的当前对象,$(this)是转为jquery对象,这样就可以使用JQUERY API,

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号