2017-04-12 60 views
0

我有這張表,我想在盤旋時改變顏色,但當鏈接位於TD內時也會改變不同的顏色。如何用CSS中TD的元素覆蓋TR?

<table> 
    <tr> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
    </tr> 

    <tr> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
    </tr> 

    <tr> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
     <td> 
      <a>HOVERING THIS LINK WILL MAKE WHOLE TR BG COLOR BLUE</a> 
     </td> 
    </tr> 
</table> 

我試着在CSS

table tr:hover:not(table td a) { 
    background-color: red !important; 
} 

table tr:hover{ 
    background-color: blue; 
} 

但我有沒有運氣。 這可能沒有使用JavaScript/JQuery的?

+0

這是不可能單獨使用CSS,CSS既然不能選擇父元素 – Johannes

+1

這可能最終成爲一個解決? https://jsfiddle.net/8h6u711v/3/ –

回答

1

您可以使用tr下的僞元素來繪製背景。

z-index將設法將它們堆在內容下。

例如:

table tr { 
 
    position: relative; 
 
} 
 

 
table tr a:before, 
 
tr:before { 
 
    content: ''; 
 
    position: absolute; 
 
    float:left; 
 
    top: 0; 
 
    left: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    z-index: -2; 
 
} 
 
table tr:hover:before { 
 
    background: red; 
 
} 
 

 
table tr a:hover:before { 
 
    z-index: -1; 
 
    background-color: blue; 
 
} 
 

 
/*and eventually: */a {display:block;background:rgba(0,0,0,0.0001);}a:hover {color:white;}
<table> 
 
    <tr> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
    </tr> 
 

 
    <tr> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
    </tr> 
 

 
    <tr> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
     <td>Hovering this part will make the whole TR bgcolor red</td> 
 
     <td> 
 
      <a>HOVERING THIS LINK WILL MAKE WHOLE TR BG COLOR BLUE</a> 
 
     </td> 
 
    </tr> 
 
</table>

0

這是不尋常的,你做沒有一些JavaScript。 但是,如果你真的想這樣做,你可以檢查這個問題,並自己嘗試。

Question 1

Question 2

希望我可以幫助...

+0

這些問題/答案是關於兄弟姐妹,而不是父母。它不會幫助:(你的答案看起來更像是一個評論比其他;) –

+0

@GCyrillus無法評論,因爲我的名譽點:)但謝謝你糾正我的答案。顯然你是對的;) – CallMeLeonardo