Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery查找html中的所有链接并将其推送到数组?_Javascript_Jquery - Fatal编程技术网

Javascript 使用jQuery查找html中的所有链接并将其推送到数组?

Javascript 使用jQuery查找html中的所有链接并将其推送到数组?,javascript,jquery,Javascript,Jquery,给定的html块是一个变量,而不是在实际文档上: var html_cmt = "Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link: https://github.com/jquery/jquery-ui Check out this

给定的html块是一个变量,而不是在实际文档上:

var html_cmt = "Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link:"
我需要构建一个包含所有链接的数组,我有以下内容,但它只是构建第一个链接,而不是全部链接

var hrefs = new Array();
hrefs.push( $("<div>").html(html_cmt).find('a').attr('href'));
关于如何创建一个包含所有链接的数组的建议,如果html内容不在文档中,只是一个JS变量?谢谢

您可以使用..映射和.get:

var hrefs = [];
$("<div>").html(html_cmt).find('a').each(function(){
    hrefs.push(this.href);
    //         ^^^^^^^^^ Resolves URLs automatically. See notes
});

但是字符串应该包含实际的元素;你说它适用于第一个元素,所以我猜你的字符串与你发布的不同。

var links=$html\u cmt.split查看这个很棒的链接:

我也这么做了。。。只是比其他人慢了很多:

var html = "Check out this awesome link: https://URL-1 Check out this awesome link: https://URL-2 Check out this awesome link: https://URL-3 Check out this awesome link:";

var urls = [];


do
{
    var start = html.indexOf("http");
    var end = 0;

    if (start > 0)
    {
        for (var i = start; i < html.length; i++)
        {
            if (html[i] == " ")
            {
                end = i;
                break;
            }
        }
        urls.push(html.substring(start, end));
    }

    html = html.substring(end, html.length);
}
while(start >= 0);

console.log(urls);

在jsfiddle上:

谢谢,我不确定你的意思,如果你想要真正的attr,请使用$this.attr'href'?@AnApprentice。我已经更新了我的答案,详细说明了这两种方法。除非您希望使用可能相对的URL,否则我建议您使用此URL。为了清晰起见,我可能希望将最终结果分配给一个变量,但在其他方面做得很好+1纳普伦蒂斯。我刚刚注意到html_cat变量不包含任何html标记。对吗?
this.href            == http://localhost/foo.bar
$(this).attr('href') == /foo.bar

this.href            == http://localhost/dir/test.php#doo
$(this).attr('href') == #doo

this.href            == http://localhost/file.do
$(this).attr('href') == ../file.do

this.href            == http://localhost/dir/
$(this).attr('href') == .
var hrefs = $("<div>").html(html_cmt).find("a").map(function() {
  return this.href;
}).get();

// find <a> elements, replace them with their hrefs, and
// convert the jQuery object to an array
var html = "Check out this awesome link: https://URL-1 Check out this awesome link: https://URL-2 Check out this awesome link: https://URL-3 Check out this awesome link:";

var urls = [];


do
{
    var start = html.indexOf("http");
    var end = 0;

    if (start > 0)
    {
        for (var i = start; i < html.length; i++)
        {
            if (html[i] == " ")
            {
                end = i;
                break;
            }
        }
        urls.push(html.substring(start, end));
    }

    html = html.substring(end, html.length);
}
while(start >= 0);

console.log(urls);