开发者

Cannot reach $(this) from within $().data()

I'm trying to set the class of my element inside a data object of the same element, but it keeps returning undefined.

$(this).data({
            orgSize:{ // Write all the sizes as data. For future reference.
                width: $(this).width(), //returns the width 开发者_JAVA百科just fine!
                height: $(this).height() //returns the height just fine!
            },
            orgClass: function(){
                cl = $(this).attr('class');
                if(cl){
                    return ' ' + cl;
                }else{
                    return ' somethingelse';
                }
            } //returns undefined
        });

        console.log($(this).attr('class')) //returns the class

edit: The problem is within the orgClass.


var me = $(this);
me.data({
  orgSize  : { width:me.width(), height:me.height() },
  orgClass : function(){
    cl = me.attr('class');
    return cl ? (' '+cl) : ' somethingelse';
  }
});


its a scope issue, 'this' inside 'orgSize' refers to the 'orgSize' object itself ..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜