0

单击选项卡时如何将背景更改为完全相同的颜色?目前,它只是保持相同的颜色。当页面加载时,是否可以让第一个选项卡的颜色与内容区域的背景颜色相同?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSS-TABS | CSS-Only "DOM TABS"</title>

<style type="text/css">

    #pagewidth { width: 760px; margin: 0 auto 0 auto; }

    #navigation { margin: 0 10em 0 0; background: #000000; color: #FFFFFF; }
    #navigation li { display: inline; padding: 0 2em 0 2em; }

    .content { background: #CCCCCC; height: 20em; padding: 1em;}

    a { color: #0066FF; }
    a:hover { color: #00CCFF; }
    a:active { font-weight:bold; }

    #container { height: 20em; overflow: hidden; }

</style>

</head>

<body>

<div id="pagewidth">

    <ul id="navigation">
        <li><a href="#c1">Content Block 1</a></li>
        <li><a href="#c2">Content Block 2</a></li>
        <li><a href="#c3">Content Block 3</a></li>
    </ul>

    <div id="container">

        <div class="content">
            <a name="c1" id="c1"></a>
            <h1>Heading 1</h1>
            <p>Here is some content, I hope that you like it!</p>
        </div>

        <div class="content">
            <a name="c2" id="c2"></a>
            <h2>Heading 2</h2>
            <p>Now that you have read content block 1, you can learn more in this block</p>
        </div>

        <div class="content">
            <a name="c3" id="c3"></a>
            <h3>Heading 3</h3>
            <p>In conclusion, content blocks are fun</p>
        </div>

    </div> <!-- end container -->

</div> <!-- end pagewidth -->

</body>
</html>
4

1 回答 1

0

你只需要多一点 HTML 和一点 CSS,就像这样:

CSS 添加:

.content .sub{ height: 20em; padding: 1em; }
...
#sub1{ background: blue; }
#sub2{ background: green; }
#sub3{ background: yellow; }

和:

<div class="sub" id="sub1">...</div>

...作为选项卡内容的包装器。

最终页面如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CSS-TABS | CSS-Only "DOM TABS"</title>
<style>
    #pagewidth { width: 760px; margin: 0 auto 0 auto; }

    #navigation { margin: 0 10em 0 0; background: #000000; color: #FFFFFF; }
    #navigation li { display: inline; padding: 0 2em 0 2em; }

    .content { background: #CCCCCC; }
    .content .sub{ height: 20em; padding: 1em; }

    a { color: #0066FF; }
    a:hover { color: #00CCFF; }
    a:active { font-weight:bold; }

    #container { height: 20em; overflow: hidden; }

    #sub1{ background: blue; }
    #sub2{ background: green; }
    #sub3{ background: yellow; }
</style>
</head>
<body>
<div id="pagewidth">

<ul id="navigation">
    <li><a href="#c1">Content Block 1</a></li>
    <li><a href="#c2">Content Block 2</a></li>
    <li><a href="#c3">Content Block 3</a></li>
</ul>

<div id="container">

    <div class="content">
        <a name="c1" id="c1"></a>
        <div class="sub" id="sub1">
        <h1>Heading 1</h1>
        <p>Here is some content, I hope that you like it!</p>
        </div>
    </div>

    <div class="content">
        <a name="c2" id="c2"></a>
        <div class="sub" id="sub2">
        <h2>Heading 2</h2>
        <p>Now that you have read content block 1, you can learn more in this block</p>
        </div>
    </div>

    <div class="content">
        <a name="c3" id="c3"></a>
        <div class="sub" id="sub3">
        <h3>Heading 3</h3>
        <p>In conclusion, content blocks are fun</p>
        </div>
    </div>

</div> <!-- end container -->

</div> <!-- end pagewidth -->

</body>
</html>

这是一个展示其工作原理的 JSFiddle:http: //jsfiddle.net/fDk3z/

于 2013-02-05T00:43:59.050 回答