2009-09-08 4 views
0

클릭 이벤트가 <tr> 인이 jQuery 코드가 있습니다. 이 이벤트는 특정 <td>을 클릭하면 실행되지 않습니다. 지금 가지고있는 코드는 훌륭하지만, 지금은 클릭했을 때 <tr>의 이벤트가 트리거되지 않아야하는 조건을 하나 더 추가하려고합니다.Jquery : a 및 selector가 있습니까?

<span></span>을 추가하려고합니다.이 버튼을 클릭하면 <tr>에서 이벤트가 트리거됩니다. 이 작동하지 않는

// Change background color on rows in new candidates list 
$(".newCandidatesTableTr td:not(#testTD) span:not(#candidateID)").click(function() { 
    $(this).parent().siblings().removeClass("diffColor"); 
    $(this).parent().toggleClass("diffColor", this.clicked); 
}); 

:

span:not(#candidateID) 

내가 이걸 어떻게해야합니까

다음

내 코드? 다음,

// Change background color on rows in new candidates list 
$(".newCandidatesTableTr td[id!=testTD] span[id!=candidateID]").click(function() { 
    $(this).parent().siblings().removeClass("diffColor"); 
    $(this).parent().toggleClass("diffColor", this.clicked); 
}); 
+0

약간의 HTML을 표시 할 수 있습니까? –

+0

복잡한 클릭 이벤트를 관리하기 위해 이벤트 위임을 사용하는 것이 좋습니다. – Berzemus

답변

1

아마 클릭 할 수있는 최상위 요소를 선택할 것입니다 클릭하지 않아야 더 특정 요소에 클릭 이벤트를 멀리 필터링 :

+0

많은 ''요소를 가지고 있다면 성능을 훨씬 향상시킬 수있는 jQuery의'live' 함수를 살펴보십시오. – Blixt

0

이보십시오.

$('tr.newCandidatesTableTr > td').click(function (e) { 
    // Get a jQuery-wrapped instance of the clicked element. 
    var target = $(e.target); 
    // Filter clicked element (due to bubbling, this is not necessarily the <td> 
    // element.) 
    if (target.is('td#testTD') || target.is('span#candidateID')) { 
     // Pass on event. 
     return true; 
    } 

    // Do stuff... 
}); 
관련 문제