2012-09-05 5 views
0

onclick 속성 함수가있는 입력 버튼이 있습니다. 나는 눌렀 던 버튼의 클래스를 변경하기 위해 호출되는 자바 스크립트 함수에서 원한다. 문제는 페이지에 여러 개의 버튼이 있기 때문에 내가 눌렀 던 버튼을 선택하기 위해 자바 스크립트 함수에 무엇을 써야하는지 잘 모르겠습니다. 아래 코드는 다음과 같습니다.Onclick JS 함수는 Element Onclick을 인식합니다.

<script type="text/javascript" > 

function load(thefile, div) {  
    $.get(thefile, function(data){ 
    $('#' + div).html(data); 
}); 
}; 

</script> 

<input type="button" class="highlighted" value="upvote" 
onclick="load('ajax_file','div')" />   

답변

2

PARAM로 패스 this 당신은 버튼을 누르면 얻을 것이다 :

<input type="button" class="highlighted" value="upvote" 
onclick="load('ajax_file','div', this)" />       

function load(thefile, div, theButton) {     
    // change class of theButton 
    var button = $(theButton); 
    button.attr("class", "yourClass"); 

    $.get(thefile, function(data){ 
     $('#' + div).html(data);   
    }); 
} 

간단한 jsfiddle 시험 here.

+0

예, $ (theButton)을 사용하십시오. addClass (....) – Nick

+0

현재 수업에 수업을 추가하려는 경우 맞습니다. 내 대답을 업데이트하여 클래스 속성을 다른 것으로 변경했습니다. – jelies

+0

감사합니다. 그것은 작동합니다! – jason328

관련 문제