2012-12-13 27 views
0

可以有人解释为什么会发生以下情况:如何通过定位锚标记中的类来更改锚标记中的颜色或文本?

<a href="#" class="test">Test</a> 

<style type="text/css"> 
.test { 
border: thin solid blue; 
color: red; 
} 
</style> 

这只是创建了边界,但使用一个类时不转的文本显示为红色。

<a href="#" id="test">Test</a> 

<style type="text/css"> 
#test { 
border: thin solid blue; 
color: red; 
} 
</style> 

为什么不类改变文字颜色,同时采用ID确实工作:

然而,当使用一个id,而不是这部作品在打开文本显示为红色?

谢谢!

+1

如果你运行这些独立的样本中的每一个像jsFiddle,他们会显示相同的东西。问题不在这里。有些东西可能会覆盖你在课堂上定义的风格。 – Shmiddty

+0

这两种方法都是一样的。见:http://jsfiddle.net/mD5us/1/ – JSW189

回答

3

使用本

demo here

<a href="#" class="test">Test</a> 

<style type="text/css"> 
a.test { 
border: thin solid blue; 
color: red; 
} 
</style> 
+0

谢谢,现在有道理! – Chain

+0

不错:),祝好运与其余 –

1

尝试改变风格的标签到这一点:

<style type="text/css"> 
    a.test{ 
     border: thin solid blue; 
     color: red; 
    } 
</style> 
+0

'a'不是'.test'的子... – Shmiddty

+0

Totaly true。它去得很快 –

2

见这个例子:http://jsfiddle.net/mD5us/4/

<div> 
    <a href="#" class="test">Test</a> 
</div> 

CSS

​body div a.test{ 
    color:yellow; 
} 
body div .test{ 
    color:brown; 
} 
body a.test{ 
    color:purple; 
} 
body .test{ 
    color: orange; 
} 
a.test{ 
    color:green; 
} 
.test { 
    border: thin solid blue; 
    color: red; 
} 

你可能会认为该链接会是红色的,但它实际上是黄色的,因为这是最具体的声明。

+0

感谢您的帮助以及 – Chain

+0

真的,这也帮了很多......我曾想过最后一个会覆盖任何类型的声明,最终影响到文本。 – Chain