2012-06-12 27 views
4
<table border=2> 
    <tr class="here1 yes"> 
     <td>aaa1</td><td>bbb1</td> 
    </tr> 
    <tr class="here2 yes"> 
     <td>aaa2</td><td>bbb2</td> 
    </tr> 

    <tr class="here55 yes"> 
     <td>aaa3</td><td>bbb3</td> 
    </tr> 
</table> 


<table border=2> 
    <tr class="here1 yes"> 
     <td>ccc1</td><td>ddd1</td> 
    </tr> 
    <tr class="here2 yes"> 
     <td>ccc2</td><td>ddd2</td> 
    </tr> 

    <tr class="here55 yes"> 
     <td>ccc3</td><td>ddd3</td> 
    </tr> 
</table> 


.yes:hover { 
    background-color: red; 
} 

LIVE:http://jsfiddle.net/KzzW8/將鼠標懸停在兩張桌子上的課程?

上面表與下面的PHP生成:

`<tr class="here<? echo $i ?> yes">` 

我想上TR.here1鼠標懸停把任何TR.here1的內容RED其中從屬TD是在組中:(aaa1,bbb1,ccc1,ddd1)不管它在哪個表中。

我相信我可以使用jQuery來做到這一點。這可能嗎?

+0

您能否以更清晰的方式解釋您想要執行的操作? – PriestVallon

+0

我不明白你的問題 –

+0

如果我把鼠標懸停在類here1上,那麼這個類應該是背景顏色:在所有類中的紅色here1在頁面上(在我的例子中在兩個表中) –

回答

5

http://jsfiddle.net/KzzW8/1/

$('tr').hover(function() { 
    var cls = $(this).prop('class').match(/here\d+/); 
    if (cls) { 
     $('.' + cls).addClass('hover'); 
    } 
}, function() { 
    $('.yes.hover').removeClass('hover'); 
});​ 

所以你懸停事件而得到here*類,並應用.hover類都具有相同的類行。在懸停時 - 您刪除所有額外添加的類

+0

可能友好的在有很多現實世界的DOM其他節點像[this](http://jsfiddle.net/KzzW8/3/)這樣做,但基本上仍然是相同的解決方案。 –

+0

@甜菜根 - 甜菜根:的確,最終決定取決於目標和當前佈局 – zerkms