jQuery 在jQuery中将所有hrefs作为数组获取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/471606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:12:29  来源:igfitidea点击:

Get all hrefs as an array in jQuery

jqueryhtml

提问by Grimace of Despair

My code looks like this:

我的代码如下所示:

<ul id="ulList">
  <li class="listClass" id="id1"><a href="http://link1">Link 1</a></li>
  <li class="listClass" id="id2"><a href="http://link2">Link 2</a></li>
  <li class="listClass" id="id3"><a href="http://link3">Link 3</a></li>
</ul>

Now I like to get the following:

现在我想得到以下内容:

All links as an array

所有链接作为一个数组

All ids of li as an array

li 的所有 id 作为数组

Can someone help me please?

有人能帮助我吗?

回答by airportyh

var ids = new Array();
var hrefs = new Array();
$('#ulList li').each(function(){
  ids.push($(this).attr('id'));
  hrefs.push($(this).find('a').attr('href'));
})

回答by Grimace of Despair

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

我知道这是旧的,但是因为我喜欢 jQuery 允许您编写的单行程序,所以我想我会添加它:

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();

回答by Venkat D.

Stumbled into this question and came up with a more reusable answer:

偶然发现了这个问题,并提出了一个更可重用的答案:

$.fn.collect = function(fn) {
    var values = [];

    if (typeof fn == 'string') {
        var prop = fn;
        fn = function() { return this.attr(prop); };
    }

    $(this).each(function() {
        var val = fn.call($(this));
        values.push(val);
    });
    return values;
};

var ids = $('#ulList li').collect('id');
var links = $('#ulList a').collect('href');

You can also pass a function into collect like so:

您还可以将函数传递给 collect ,如下所示:

var widths = $('#ulList li').collect(function() {
    return this.width();
});

回答by nickf

This should work.

这应该有效。

var ids = [],
    hrefs = []
;   
$('#ulList')
    .find('a[href]')  // only target <a>s which have a href attribute
        .each(function() {
            hrefs.push(this.href);
        })
    .end()
    .find('li[id]')   // only target <li>s which have an id attribute
        .each(function() {
            ids.push(this.id);
        })
;

// ids = ['id1', 'id2', 'id3']
// hrefs = ['http://link1', 'http://link2', 'http://link3']

回答by Mauricio Wolff

var links = [], ids = [];
var $ul = $('#ulList');
var $lis = $ul.children('li');
var $as = $lis.children('a');

for(var count = $lis.length-1, i = count; i >= 0; i--){
    ids.push($lis[i].id);
    links.push($as[i].href);
}

回答by Daniel Lizik

If you like one liners and hate having to instantiate an empty array.

如果你喜欢一个衬垫并且讨厌必须实例化一个空数组。

[]
  .slice
  .call($('#ulList a'))
  .map(el => el.getAttribute('href'))