-2
<body>
<div id="scrollup">
<div class="link">
<br><a href="http://www.google.com/">Google1</a>
<br><a href="http://www.yahoo.com/">Yahoo</a>
</div>
<div class="link">
<br><a href="http://www.Microsoft.com/">Microsoft</a>
<br><a href="http://www.Apple.com/">Apple</a>
</div>
<div class="link">
<br><a href="http://www.cisco.com/">Cisco</a>
<br><a href="http://www.Dell.com/">Dell</a>
</div>
</div>
</body>

JQuery Appendを使用してHrefのリストをクラスに追加したい、ハイパーリンクのリストを配列に格納していて、それらをグループに追加したい、コードはhrefを定義する方法を示しています

また、配列にハイパーリンクがいくつあるかわからないことに注意してください。また、(class = "link")の数は、追加することでハイパーリンクの数に依存します(たとえば、各クラスに4つのhref)

4

1 に答える 1

0

これはどう

<!doctype html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Demo</title>
            <script src="jquery-1.8.3.min.js"></script>

            <script>
                var links = ['http://stackoverflow.com/questions/13601373/jquery-append-to-div',
                        'http://jsfiddle.net/VCjLX/',
                        'https://www.google.at/'];


                $(function () {
                    //get all containers where we append the links
                    var containers = $('.link');
                    //iterate over all links
                    $(links).each(function () {
                        var link = $('<a>', {
                            href: this,
                            text: this
                        });
                        //append the link to the containers (it gets appened to all elements!)
                        containers.append(link);
                        //append the line break (again to all elements)
                        containers.append('<br />');
                    });
                });

            </script>
        </head>
        <body>
            <div class="link"></div>
            <br />
            <div class="link"></div>
        </body>
    </html>

フィドルのデモ: http: //jsfiddle.net/R2kTC/

于 2012-11-28T09:15:46.313 に答える