How to get all the links of a list inside a div?

后端 未结 4 1222
天命终不由人
天命终不由人 2020-12-13 00:20

I have a code with the following DOM Tree:

4条回答
  •  囚心锁ツ
    2020-12-13 01:23

    $('#blogPagination').find('a').attr('href');
    

    This should find all a elements in the specified area, the get the href of them, assuming that you've already got jQuery and all that good stuff set up.

    If you have multiple a elements, you could do something like this:

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

    This will print out each href of each a in that div.

    If you need to prevent the link from changing the page, you need to add a click handler to the a elements.

    $('#blogPagination').on('click', 'a', function(e) {
        e.preventDefault();
        console.log($(this).attr('href'));
    });
    

    This will prevent the user from being taken to the link, and get the href of the link when clicked.

    Is this what you want?

提交回复
热议问题