2014-10-05 2 views
0

나는 extend.js로 놀고 있는데 jQuery의 $.each과 함께 문제가 발생하여 문자열에 추가됩니다.

var Test = Class.extend(function(){ 
    this.dom = '<div '; 
    this.class; 
    this.id; 

    this.add_class = function(){ 
     this.dom += 'class = "'; 
     $.each(this.class, function(i,e){ 
      console.log(e); 
      this.dom += e; 
      this.dom += ' '; 
     }); 
     this.dom += '"'; 
     return this; 
    }; 

    this.add_id = function(){ 
     this.dom += 'data-id = "'+this.id+'" '; 
     return this.dom; 
    }; 

}); 

var tester = new Test(); 
tester.class = ["coolClass", "TESTER"]; 
tester.id = "coolId"; 
console.log(tester.add_class().add_id()); 

콘솔 내 수업이 비어있는 이유

coolClass 
TESTER 
<div class = "" data-id = "coolId" 

난 그냥 볼 수 없습니다 보여줍니다?

답변

1

상황처럼 뭔가를해야 할 것입니다.

$ .each 콜백 내에서 this은 현재 반복되는 항목입니다. Test 인 것은 this 개체가 아닙니다.

사용이 :

var oThis = this; 
$.each(this.class, function(i,e){ 
    console.log(e); 
    oThis.dom += e; 
    oThis.dom += ' '; 
}); 
관련 문제