使用jQuery為部落格組建目錄

來源:互聯網
上載者:User

標籤:blog   http   os   io   使用   java   ar   for   div   

這段代碼展示了如何為div#content中的內容組建目錄,也無非是對h系列標記進行解析。當然,也早有一些人實現了。
?1. [代碼][HTML]代碼     
<html>
    <head>
        <title> 測試  </title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript" src="../lib/js/jquery-1.7.1.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            $(document).ready(function() {
                //index01
                $("#index01").text($("#content").html());
                //index02
                x = $("h1,h2").toArray();
                c = "";
                for (i = 0; i < x.length; i++)
                {
                    c = c + x[i].innerHTML + "<br />";
                }
                $("#index02").html(x + "<br/>" + c);
                //index03
                x = $("h1,h2");
                c = "";
                x.each(function() {
                    c = c + $(this).text() + "  " + $(this).prop(‘tagName‘) + "<br/>"; //prop()與attr()的區別
                });http://www.huiyi8.com/hunsha/lifu/?
                $("#index03").html(c);
                //index04
                //generate anchor
                x = $("h1,h2");
                n = 0;
                c = "";
                x.each(function() {
                    $(this).before("<a name=‘anchor" + n + "‘></a>");
                    if ($(this).prop(‘tagName‘) == "H1") //不可用"h1"
                    {婚紗禮服欣賞        
                        c=c+"<a href=‘#anchor"+n+"‘ class=‘anchor-h1‘>"+$(this).text()+"</a><br/>";
                    }
                    else {
                        c=c+"<a href=‘#anchor"+n+"‘ class=‘anchor-h2‘>"+$(this).text()+"</a><br/>";
                    }
                    n = n + 1;
                });
                $("#index04").html(c);
            });
        </script>
        <div id="index01"></div><hr/>
        <div id="index02"></div><hr/>
        <div id="index03"></div><hr/>
        <div id="index04"></div><hr/>
        <div id="content">
            <h1>第一章</h1>
            <h2>第1節</h2>
            <p>你好</p>
            <h2>第2節</h2>
            <h1>第二章</h1>
            <h2>第1節</h2>
            <h2>第2節</h2>
        </div>
    </body>
</html>
?

2. [圖片] 01.jpg    

使用jQuery為部落格組建目錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.