2014-02-27 2 views
0

HTML은 요소를 끈 상태로 토글하는 단추 목록입니다.JQuery - on()을 사용하여 다른 텍스트를 토글하면서 버튼의 텍스트를 변경합니다.

단추의 텍스트를 "표시"에서 "숨기기"로 변경하고 싶습니다. 나는 그것이 complete() 콜백을 통해 수행되기를 원한다. 클릭 한 요소에 어떻게 액세스 할 수 있습니까? 내가 가진

이 JS 코드 :

$("body").on("click", ".selector", function(eve) { 
    $(".selector", $(this).parent()).toggle(function(){ 
     $(/*here comes the element that was clicked*/).text(
      function(i,text){ 
       return text == "show" ? "hide" : "show"; 
      } 
     ); 
    }); 
    }); 

답변

3

스토어 참조

$("body").on("click", ".selector", function(eve) { 
var that = $(this); 
$(".selector", $(this).parent()).toggle(function(){ 
    that.text(
     function(i,text){ 
      return text == "show" ? "hide" : "show"; 
     } 
    ); 
}); 
}); 
+0

그것은 그래도 정확 내가 잘못 사용했기 때문에 나는 또 다른 문제를 안고 있었다. - 대신에'text()'를 사용했다. e'val()'함수. 감사 – yossi

0

당신은 event의이 event.currentTarget 재산 사용할 수 있습니다 this

$("body").on("click", ".selector", function (eve) { 
    $(this).parent().find(".selector").toggle(function() { 
     $(eve.currentTarget).text(function (i, text) { 
      return text == "show" ? "hide" : "show"; 
     }); 
    }); 
}); 
관련 문제