2011-12-13 2 views
1

나는 다른 번호를 각각 계산 카운터를 만들어,하지만 난 그것을 나던 작업 ... 클래스의 모든 elemnts을 선택할 때JQuery와 각 선택 나던 작업

$(document).ready(function() { 


    function change() { 
     /* 
     $(".test").each(function(i,domeElement){ 

      var theNum = parseInt(this.html())+1; 
      this.html(theNum); 
     }); 
     */ 

     //this works... the other one doesnt why?!?! 
     var theNum = parseInt($(".test").html()) + 1; 
     $(".test").html(theNum); 
    } 

    setInterval(change, 1000); 

}); 

http://jsfiddle.net/DTyY7/

+1

질문에 JSFiddle에 * * 접속하지 마십시오. 항상 질문에 코드를 포함 시키십시오. – Matt

답변

2

html()이 (.each() 내부)는 jQuery를 기능과 this이기 때문에 당신은 domElement입니다

$(this).html(theNum); 

하지

this.html(theNum); 

를 사용한다 그래서 당신은 jQuery를로 포장해야한다 개체

피들 여기 :당신의 each() 콜백 몸에

+0

는 3 시간 만 낭비했습니다 :) –

0

는 첫째을 적용 모두 .test의 인스턴스입니다. 모든 요소를 ​​반복하고 +1을 별도로 적용해야합니다.

$('.test').each(function(){ 
      $(this).html(parseInt($(this).html())+1) 
     });
0
$(document).ready(function() { 
    function change() { 
     $(".test").each(function(i,domeElement) { 
      var theNum = parseInt($(this).html())+1; 
      $(this).html(theNum); 
     }); 
    } 
    setInterval(change,1000); 
}); 
1

this$(this) 변경되어야합니다

$(".test").each(function(){ 
    var theNum = parseInt($(this).html())+1 || 0; 
    $(this).html(theNum); 
}); 

this 데모를 참조하십시오.

0
$(document).ready(function(){ 


function change(){ 

    $(".test").each(function(i, domElement){ 

     //var theNum = parseInt(this.html())+1; 
     var num = parseInt($(".test").html()); 
     num++ 
     $(".test").html(num); 
    }); 


    //this works... the other one doesnt why?!?! 
     /* var theNum = parseInt($(".test").html())+1; 
     $(".test").html(theNum); 
*/} 

setInterval(change,1000); 

});

수정 방법입니다. 당신이 keword의 다른 범위에 있기 때문에 이것은 작동하지 않습니다.