admin管理员组文章数量:1022982
given a block of html that is a var, not on the actual document:
var html_cmt = "Check out this awesome link: Check out this awesome link: Check out this awesome link: Check out this awesome link:"
I need to build an array of all links, I have the following but it's only building the first one, not all.
var hrefs = new Array();
hrefs.push( $("<div>").html(html_cmt).find('a').attr('href'));
Suggestions on how I can create an array of all the links, given html content that is not on the document just a JS var? thank you
given a block of html that is a var, not on the actual document:
var html_cmt = "Check out this awesome link: https://github./jquery/jquery-ui Check out this awesome link: https://github./jquery/jquery-ui Check out this awesome link: https://github./jquery/jquery-ui Check out this awesome link:"
I need to build an array of all links, I have the following but it's only building the first one, not all.
var hrefs = new Array();
hrefs.push( $("<div>").html(html_cmt).find('a').attr('href'));
Suggestions on how I can create an array of all the links, given html content that is not on the document just a JS var? thank you
Share Improve this question asked Jan 12, 2012 at 21:36 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges 1-
AnApprentice. I just noticed that your
html_cat
variable doesn't contain any HTML tags. Is that correct? – Rob W Commented Jan 12, 2012 at 21:49
4 Answers
Reset to default 5var hrefs = [];
$("<div>").html(html_cmt).find('a').each(function(){
hrefs.push(this.href);
// ^^^^^^^^^ Resolves URLs automatically. See notes
});
Another approach:
var hrefs = $("<div>").html(html_cmt).find('a').map(function(){
return this.href;
}).toArray();
Notes
this.href
will return the fully resolved UR, $(this).attr('href')
will return the real attribute.
Example (assume http://localhost/dir/test.php
):
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') == .
You can use ..map
and .get
: http://jsfiddle/tyggq/.
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
The string should contain actual <a>
elements though; you say it works for the first <a>
element so I guess your string is different from what you posted.
I did it too... just a lot slower then everyone else :)
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);
on jsfiddle: http://jsfiddle/HfvyE/
var links = $html_cmt.split("Check out this awesome link: ")
given a block of html that is a var, not on the actual document:
var html_cmt = "Check out this awesome link: Check out this awesome link: Check out this awesome link: Check out this awesome link:"
I need to build an array of all links, I have the following but it's only building the first one, not all.
var hrefs = new Array();
hrefs.push( $("<div>").html(html_cmt).find('a').attr('href'));
Suggestions on how I can create an array of all the links, given html content that is not on the document just a JS var? thank you
given a block of html that is a var, not on the actual document:
var html_cmt = "Check out this awesome link: https://github./jquery/jquery-ui Check out this awesome link: https://github./jquery/jquery-ui Check out this awesome link: https://github./jquery/jquery-ui Check out this awesome link:"
I need to build an array of all links, I have the following but it's only building the first one, not all.
var hrefs = new Array();
hrefs.push( $("<div>").html(html_cmt).find('a').attr('href'));
Suggestions on how I can create an array of all the links, given html content that is not on the document just a JS var? thank you
Share Improve this question asked Jan 12, 2012 at 21:36 AnApprenticeAnApprentice 111k202 gold badges637 silver badges1k bronze badges 1-
AnApprentice. I just noticed that your
html_cat
variable doesn't contain any HTML tags. Is that correct? – Rob W Commented Jan 12, 2012 at 21:49
4 Answers
Reset to default 5var hrefs = [];
$("<div>").html(html_cmt).find('a').each(function(){
hrefs.push(this.href);
// ^^^^^^^^^ Resolves URLs automatically. See notes
});
Another approach:
var hrefs = $("<div>").html(html_cmt).find('a').map(function(){
return this.href;
}).toArray();
Notes
this.href
will return the fully resolved UR, $(this).attr('href')
will return the real attribute.
Example (assume http://localhost/dir/test.php
):
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') == .
You can use ..map
and .get
: http://jsfiddle/tyggq/.
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
The string should contain actual <a>
elements though; you say it works for the first <a>
element so I guess your string is different from what you posted.
I did it too... just a lot slower then everyone else :)
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);
on jsfiddle: http://jsfiddle/HfvyE/
var links = $html_cmt.split("Check out this awesome link: ")
本文标签: javascriptUsing jQuery to find push all links in html to an arrayStack Overflow
版权声明:本文标题:javascript - Using jQuery to find push all links in html to an array? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745570870a2156728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论