2011-08-23 3 views
2

나는이 기능을 사용하고 있습니다 :페이지가로드 될 때 및 링크를 클릭 할 때 jQuery 함수를 트리거링 하시겠습니까?

$(document).ready(function() { 

$('.gdthumbtext') 
.contents() 
.filter(function(){ 
    return this.nodeType == 3; 
}).each(function(){ 
    this.nodeValue= this.nodeValue.replace(/\+/g,''); 
}); 

}); 

.gdthumbtext DIV 내부 번호 앞에 더하기 기호를 제거합니다. 이제 문제는 번호가 클래스 내가 링크를 클릭하면 해당 함수를 다시 호출 (또는 리콜) 할 생각 .gdt-starrating.

와 링크 를 클릭 아약스와 함께 각각의 시간을 변경하는 것입니다.

아마도 이런 것이 필요할 것입니다 : .gdt-starrating 링크가 클릭되면 페이지가로드 될 때 또는이로드 될 때 함수를 호출하십시오.

어떻게 할 수 있습니까?

답변

1

코드를 자체 함수로 옮기고 해당 함수를 onload로 호출 한 다음 링크를 클릭 할 때마다 함수를 계속 호출 할 수 있습니다. 이와 같이 :

$(document).ready(function() { 
    // Call cleanup onload 
    stripPlusSigns(); 

    $('a.gdt-starrating').click(function() { 
     // Do stuff here... 
     // ... 

     // Strip plus signs again 
     stripPlusSigns(); 
    }); 
}); 

function stripPlusSigns() 
{ 
    $('.gdthumbtext') 
     .contents() 
     .filter(function(){ 
      return this.nodeType == 3; 
     }).each(function(){ 
      this.nodeValue= this.nodeValue.replace(/\+/g,''); 
     }); 

    return true; 
} 
+0

이상한 제안 사항은 없습니다. – alexchenco

+0

코드에 대한 링크를 제공 할 수 있습니까? –

0

당신은 맞는 생각이 있습니다.

ajax 콘텐츠를로드 한 후에 ajax Sucess에서 실행해야합니다. 나는 click과 ajax를 모두 포함시켰다.

$(document).ready(function() { 

    // run on page load 
    removeSigns(); 

    // you could run on ajax load 
    $('.gdt-starrating').bind('click', function() { 
     removeSigns(); 
    }); 

    // you should be running it on Ajax success callback 
    $.ajax({ 
      // your normal ajax stuff... 

     success : function() { 
      removeSigns(); 
     } 
    }); 

}); 


function removeSigns() { 
    $('.gdthumbtext') 
    .contents() 
    .filter(function(){ 
     return this.nodeType == 3; 
    }).each(function(){ 
     this.nodeValue= this.nodeValue.replace(/\+/g,''); 
    } 

); }

관련 문제