2013-07-03 68 views
1

我想將css應用於表內的錨標記。但我無法看到任何變化。 如何做到這一點?如何將css應用於表格內的錨標記

代碼:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
table.tab>td>a 
{ 
background-color:yellow; 
} 
</style> 
</head> 

<body> 
<table class="tab"> 
<tr><td><a href="#">google.com</a></td></tr> 
</tr></table> 
</body> 
</html> 
+1

規定'''是子組合子 - 但'td'不是'table'的子元素。 (即使你在那裏增加了一個'tr',它可能仍然不起作用,因爲這些瀏覽器之間的'tbody'元素將隱含地創建。) – CBroe

+0

爲什麼不只是爲' '? – Nitesh

回答

4
table.tab td a{ 
background-color:yellow;} 
0
table a 
{ 
background-color:yellow; 
} 

這應該解決您的問題。

1

正確關閉您的表格,並使用不帶>的選擇器。所以:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
table.tab td a 
{ 
background-color:yellow; 

} 
</style> 
</head> 

<body> 
<table class="tab"> 
<tr><td><a href="#">google.com</a></td></tr> 
</table> 
</body> 
</html> 
1
table.tab td a 
{ 
    background-color:yellow; 
} 

>選擇由「父母」指定元素的「小孩」中規定的所有直接子元素。

ChildSelector API

1

你的代碼幾乎是正確的。這個工作對我來說:

<style type="text/css"> 
    table.tab td a 
    { 
     background-color: yellow; 
    } 
</style> 
<table class="tab"> 
    <tr> 
     <td> 
      <a href="#">google.com</a> 
     </td> 
    </tr> 
</table> 
1

你必須做如下所示:

table.tab td a { 

    background-color: yellow; 

} 

一個「>」選擇由child指定的所有直接子元素被parent