2013-12-10 3 views
0

'See me'를 클릭하면 'see me see'라고 표시됩니다.
나는 이것을 $ ('div')로 할 수있다. ... 클릭하면 잘 작동한다. 하지만 컨테이너 div를 사용하는 것이 더 바람직하다고 생각합니다. 전체 페이지가 긴 페이지이고 함수가 onclick에 대해 div를 사용하면 클릭 만하면 실행됩니다.Jquery div 클래스를 Onclick div로 내부 div에서 클릭하면 표시

<div class="directFilter"> 
    <div class="boxWithRightArrow seeMe">See me</div> 
    <div class="boldFont filterHeader">Sort By</div> 
    <div class="boxWithRightArrow borderTop bestClick">Best click</div> 
    <div class="boxWithRightArrow intermediateTime">Intermediate time</div> 
    <div class="boxWithRightArrow shortestTime">Shortest time</div> 
    <div class="boxWithRightArrow iPreferred">I preferred</div> 
</div> 


$('.directFilter').click(function() { 

     alert($(this).attr('class')); //alerts directFilter 

      if ($(this).hasClass("seeMe")) { 
       alert("see me"); 
      } else if ($(this).hasClass("bestClick")) { 
       alert("b click"); 
      } else if ($(this).hasClass("intermediateTime")) { 
       alert("itime"); 
      } else if ($(this).hasClass("shortestTime")) { 
       alert("s time"); 
      } else if ($(this).hasClass("iPreferred")) { 
       alert("i pre"); 
      } 
} 
    ); 

JsFiddle : http://jsfiddle.net/smilyface/e2L7s/

어떤 제안?

답변

6

event.target을 사용하여 이벤트 소스를 식별 할 수 있습니다. 요소의 내부 내용을 가져 오려면 $(event.target).html() 또는 $(event.target).text()과 같은 html() 또는 text()를 사용해야합니다.

$('.directFilter').click(function(event) { 
     alert($(event.target).html()); //alerts directFilter 
}); 

당신이 다음 조건에서 소스 개체 식별로 event.target 사용할 필요가있는 경우 각에서 할 수있는 유일한 일이있는 경우

Live Demo.

+0

좋아! 잘 작동합니다. 감사합니다 :) 8 분 후에 만 ​​답변으로 설정할 수 있습니다 (틱 마크)! – smilyface

+0

이것은 내가 원한 것이다 : alert ($ (event.target) .hasClass ('seeMe')); – smilyface

+0

그러면 다른 조건은 어떻습니까? – Adil

3
$(document).on('click',function(e) { 
    // use this if you want limit clicks in .directFilter only 
    // $(document).on('click','.directFilter',function(e) 

    $this = $(e.target); 
    alert($this.text()) 
}); 
2

http://jsfiddle.net/prollygeek/e2L7s/7/

$('.directFilter').children().click(function() { 

     // alert($(this).attr('class')); //alerts directFilter 

      if ($(this).hasClass("seeMe")) { 
       alert("see me"); 
      } else if ($(this).hasClass("bestClick")) { 
       alert("b click"); 
      } else if ($(this).hasClass("intermediateTime")) { 
       alert("itime"); 
      } else if ($(this).hasClass("shortestTime")) { 
       alert("s time"); 
      } else if ($(this).hasClass("iPreferred")) { 
       alert("i pre"); 
      } 
} 
    ); 

많은 IF를를 사용하는 이유는 무엇입니까?!

관련 문제