日期:2014-05-17  浏览次数:20673 次

请教高手,如何对许多段落单独设置各个样式?
大家好,我想问下,我有一段文字,有很多<p>

我现在对第一个<p> 单独设置了class ,采用first-child 这种方法来对第一个<p>设置。

但是现在我想对第二个,或者剩余其他的所有段落设置样式,请问如何设置? 采用什么方法。

我想这样实现,第一个p 的样式为class1 , 其余的p 的样式全部为class2.

请问这里有人会吗?

------解决方案--------------------
<!doctype html>
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>

<style type="text/css">
p:first-child { background-color:#CCC}
p { background-color:#ABC}
</style>
</head>

<body>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
</body>
</html>

------解决方案--------------------
<html>
<head>
<meta charset="gb2312">
<title>无标题文档</title>

<style type="text/css">
.p_detail { background-color:#CCC}
.p_excerpt { background-color:#ABC}
</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
    $("p:first-child").addClass("p_detail")
    $("p:not(:first-child)").addClass("p_excerpt")
});
</script>

</head>
 
<body>
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
</body>
</html>