[CSS3] CSS :target Selector

本文介绍如何使用 CSS 的 :target 伪类选择器来控制页面中带有 hash 标识符的内容显示与隐藏。通过简单的 HTML 结构与 CSS 规则实现 Tab 切换效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The :target selector allows us to interact with a fragment identifier, or hash, in our URL from CSS.

 

HTML:

<body>
    <a href="#tab1">Tab 1</a><a href="#tab2">Tab 2</a><a href="#tab3">Tab 3</a>
    <div id="tab1" class="tab">
        <h1>Tab 1 Content</h1>
    </div>
    <div id="tab2" class="tab">
        <h1>Tab 2 Content</h1>
    </div>
    <div id="tab3" class="tab">
        <h1>Tab 3 Content</h1>
    </div>
</body>

 

CSS:

    <style>
        .tab{
            display: none;
        }
        .tab:target {
            display: block;
        }
    </style>

 

 

 

转载于:https://www.cnblogs.com/Answer1215/p/5468599.html

代码片段:   $(function () {         //评分         var starRating = 0;         $(&#39;.photo span&#39;).on(&#39;mouseenter&#39;,function () {             var index = $(this).index() 1;             $(this).prevAll().find(&#39;.high&#39;).css(&#39;z-index&#39;,1)             $(this).find(&#39;.high&#39;).css(&#39;z-index&#39;,1)             $(this).nextAll().find(&#39;.high&#39;).css(&#39;z-index&#39;,0)             $(&#39;.starNum&#39;).html((index*2).toFixed(1) &#39;分&#39;)         })         $(&#39;.photo&#39;).on(&#39;mouseleave&#39;,function () {             $(this).find(&#39;.high&#39;).css(&#39;z-index&#39;,0)             var count = starRating / 2             if(count == 5) {                 $(&#39;.photo span&#39;).find(&#39;.high&#39;).css(&#39;z-index&#39;,1);             } else {                 $(&#39;.photo span&#39;).eq(count).prevAll().find(&#39;.high&#39;).css(&#39;z-index&#39;,1);             }             $(&#39;.starNum&#39;).html(starRating.toFixed(1) &#39;分&#39;)         })         $(&#39;.photo span&#39;).on(&#39;click&#39;,function () {             var index = $(this).index() 1;             $(this).prevAll().find(&#39;.high&#39;).css(&#39;z-index&#39;,1)             $(this).find(&#39;.high&#39;).css(&#39;z-index&#39;,1)             starRating = index*2;             $(&#39;.starNum&#39;).html(starRating.toFixed(1) &#39;分&#39;);             alert(&#39;评分:&#39; (starRating.toFixed(1) &#39;分&#39;))         })
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值