Wie bekomme ich alle Links einer Liste in einem Div?

Ich habe einen Code mit dem folgenden DOM-Baum:

<div id="blogPagination">
    <div class="pagination">
        <ul>
            <li>
                <a href="/2" >1</a>
            </li>
            <li>
                <a href="/3" >2</a>
            </li>
        </ul>
    </div>
</div>

Ich versuche die Href meines Tags zu erreichen. Ich kann es mit nichts erreichen, was ich versucht habe.

Was ist der beste Weg, um es mit jQuery zu erreichen?

Ich habe es versucht:

console.log($('#blogPagination div ul > li a ').attr("href"));

console.log($('#blogPagination > a ').attr("href"));

$('#blogPagination').children('a')

console.log($('#blogPagination div ul li a').attr("href"));

ohne glück ..

Vielen Dank

BEARBEITEN:

Nach der Antwort von nbrooks habe ich Folgendes versucht:

function bindPagination() {

    console.log("bind");

    $(function() {
        var links = $("#blogPagination ul a").map(function(e) {
        e.preventDefault();
            return this.href;
        }).get();
        console.log(links);
});

EDIT 2:

In Anbetracht der Antwort von Syfaro habe ich auch versucht:

$('#blogPagination').find('a').each(function(e) {
    e.preventDefault();
    console.log($(this).attr('href'));
});

Ohne glück

EDIT 3: Ich möchte weitere Details zu dieser Funktion angeben, die sich schließlich erheblich auswirken können:

Um diese Paginierung zu laden, verwende ich Ajax und Lenker, die in eine dokumentenfertige Funktion eingebunden sind:

$(document).ready(function(){

    // Get the customer service stats
    var Content = {

    init: function() {

            /* this.getHomePosts(); */
            this.getBlogPosts();
        },

    getBlogPosts: function(offset) {
        if(offset == undefined){
            offset = 0;
        }
        // GET the events with JSON
        $.ajax({
            type: "POST",
            data: {},
            url: site_url+"/main/blog/"+offset,
            dataType: "json",
            success: function(results) {
                posts = results["posts"].map(function (blogContent) {
                    if( blogContent.picture != '' ) {
                        return {
                            Title: blogContent.title ,
                            Picture: Content.urlPostPic + blogContent.picture ,
                            Video: '' ,
                            Text: blogContent.text ,
                            Datetime: blogContent.datetime ,
                        }
                    } else {
                        return {
                            Title: blogContent.title ,
                            Picture: '' ,
                            Video: blogContent.video ,
                            Text: blogContent.text ,
                            Datetime: blogContent.datetime ,
                        }
                    }
                });

                pagination = {pagination: results["pagination"]};

                var template = Handlebars.compile( $('#templateBlog').html() );
                $('#blogPosts').append( template(posts) );

                var template = Handlebars.compile( $('#templatePagi').html() );
                $('#blogPagination').append( template(pagination) );
                                    // Here we call bindPagination <===
                bindPagination();
            }
        });
    },

};

Content.init();

Sie können in der Funktion get BlogPosts sehen, dass ich BindPagination aufrufe, was diese Funktion sein soll, um das Standardverhalten zu verhindern und den Inhalt abhängig vom Offset aufzurufen (Paginierungssystem).

function bindPagination() {

    console.log("bind");


    var links = $("#blogPagination ul a").map(function(e) {
        e.preventDefault();
        return this.href;
    }).get();
    console.log(links);

    $('#blogPagination').find('a').each(function(e) {
        console.log("clicked !");
        e.preventDefault();
        console.log($(this).attr('href'));

     //    var attr = this.attr();
        // var id = attr.replace("/","");

        // $('#blogPosts').empty();
        // $('#blogPagination').empty();
        // Content.getBlogPosts(id);
    });
}
});

das Letzte }); stehe für das Ende des Dokuments bereit.

Antworten auf die Frage(3)

Ihre Antwort auf die Frage