Get all hrefs as an array in jQuery

后端 未结 8 1299
[愿得一人]
[愿得一人] 2020-12-17 20:41

My code looks like this:

  • Link 1
  • <
相关标签:
8条回答
  • 2020-12-17 21:00
    var ids = new Array();
    var hrefs = new Array();
    $('#ulList li').each(function(){
      ids.push($(this).attr('id'));
      hrefs.push($(this).find('a').attr('href'));
    })
    
    0 讨论(0)
  • 2020-12-17 21:00

    I know this is old, but as I like the oneliners that jQuery allows you to write, I thought I'd add it:

    var allLinks = $('#ulList a').map(function(i,el) { return $(el).attr('href'); }).get();
    var allIds = $('#ulList li').map(function(i,el) { return $(el).attr('id'); }).get();
    
    0 讨论(0)
提交回复
热议问题