2010-05-23 99 views
3

我想爲表格行中的2個不同的TD創建切換事件。 該事件應該顯示/隱藏下一個表格行。在jQuery中爲兩個不同的元素創建相同的事件(不是爲每個元素都爲!)

<table> 
    <tr> 
     <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> <td class="clickable1">6</td> <td class="clickable2">7</td> 
    </tr> 
    <tr> 
     <td>this row should be toggled between show/hide once one of the clickable TDs were clicked</td> 
    </tr> 
</table> 

這是我嘗試應用的代碼,但它應用了類中的每一個事件:

$('.clickable1,.clickable2').toggle(function() { 
    $(this).parent() 
     .next('tr') 
     .show(); 
}, function() { 
$(this).parent() 
     .next('tr') 
     .hide(); 
}); 

一件事:我申請上的每個TR一個CSS懸停僞類。我怎樣才能使兩個TR被突出顯示(像兩個懸停效果一樣)?

這裏是我發現完全到目前爲止奏效:

$('.clickable1,.clickable2').click(function() { 
    $(this).parent() 
      .next('tr') 
      .toggle(); 
}); 

看來,TR記得它之前的狀態通過切換命令!

+0

您應該創建一個答案,將其標記爲有效。否則,這個問題會一直出現在「未回答的問題」列表中。 – kikito 2010-05-26 08:40:15

+0

好吧,我剛剛添加了答案 我如何將其標記爲有效? – danfromisrael 2010-05-26 09:01:23

回答

0

感謝所有的答案傢伙! 下面是我找到了完美迄今進行的工作:

$('.clickable1,.clickable2').click(function() { 
         $(this).parent() 
           .next('tr') 
           .toggle(); 
}); 
1

而不是使用切換處理程序,只需根據按鈕單擊切換可見性。使用toggleClass切換「懸停」類。

$('.clickable1,.clickable2').click(function() { 
    var parent = $(this).parent(); 
    parent.toggleClass('hovered'); 

    var next = parent.next('tr'); 
    next.toggle().toggleClass('hovered'); 
}); 
+0

假設顯示/隱藏是切換所需的唯一功能,這是一個很好的解決方案。 – user113716 2010-05-23 16:18:07

0

Check this jsFiddle out演示兩個請求的行爲。請注意,我將類clickable1和clickable2更改爲可點擊,因爲在您的示例中沒有看到任何理由有兩個類。如果你願意的話,可以稍微調整一下。下面還包含代碼的情況下的jsfiddle死亡:

HTML:

<table id="table"> 
<tr> 
<td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> <td class="clickable">6</td> <td class="clickable">7</td> 
</tr> 
<tr class="toggleVis"><td colspan="7">this row should be toggled between show/hide once one of the clickable TDs were clicked</td></tr> 
</table>​ 

CSS:

.clickable{cursor:pointer;} 
.hide{display:none;} 
.hover{font-weight:bold;border:solid 1px blue;background-color:yellow;}​ 

的jQuery:

$(".clickable").click(function() { 
    $(".toggleVis").toggleClass("hide"); 
}); 
$('tr', "#table").hover(
    function() { 
     $(this).toggleClass("hover"); 
    }, 
    function() { 
     $(this).toggleClass("hover"); 
    }); 
+0

恭敬地說,這並不完美。首先,請記住OP在事件處理之外可能有兩個不同類的原因。其次,你的點擊處理程序會使用'.toggleVis'類切換* all *元素,而OP只是想切換下一行(假設他有幾行對)。第三,對懸停的行來說,OP希望用它的對(隱藏在點擊事件中的那一對)對每行進行調整。 – user113716 2010-05-23 16:39:30

1

如果你真的只想要一個事件,一種選擇是將處理程序放在父容器上,並依靠自動冒泡來觸發它。

您可以正常使用jQuery的delegate()函數,但我不認爲它支持toggle事件。

$('tr:has([class^=clickable])').toggle(function(e) { 

     // Verify that the target had a clickable class 
    if($(e.target).closest('[class^=clickable]').length) { 

      // No need to call .parent() since the handler is now on the <tr> 
     $(this).next('tr').show(); 
    } 
}, 
function(e) { 

     // Verify that the target had a clickable class 
    if($(e.target).closest('[class^=clickable]').length) { 

      // No need to call .parent() since the handler is now on the <tr> 
     $(this).next('tr').hide(); 
    } 
}); 

或者,你可能只有一個火其他。

$('.clickable1').toggle(function() { 
     $(this).parent() 
      .next('tr') 
      .show(); 
    }, function() { 
    $(this).parent() 
      .next('tr') 
      .hide(); 
    }); 

$('.clickable2').click(function() { 
    $(this).prev().click();  // Fires the click event for its previous sibling 
}); 

對於懸停,這將是一個方法。

$('tr:has([class^=clickable]):even').hover(
    function() { 
     $(this).addClass('hilite') 
     .next().addClass('hilite'); 
    }, 
    function() { 
     $(this).removeClass('hilite') 
     .next().removeClass('hilite'); 

}); 
0

爲什麼你不能這樣做?

$('.clickable1,.clickable2').click(function(e){ 
    var nTR=$(e.target).parent().next('tr'); 
    if (nTR.css("display")!="none") nTR.hide(); 
    else nTR.show(); 
});​​​​​​ 

此外,你可能想 take a look at this answer

至於TR上的psuedo-class,你將不得不使用另一個事件處理程序,因爲這是一個自定義行爲。幸運的是,這很容易:

$("tr").hover(function(){ 
    $(this).next('tr').andSelf() 
    .css("backgroundColor","#F00");}, 
    function(){$(this).next('tr').andSelf() 
    .css("backgroundColor","#FFF"); 
}); 
相關問題