Javascript jQuery:获取目的地和内容

Javascript jQuery:获取目的地和内容,javascript,jquery,Javascript,Jquery,我有一些数组,从类似的“a”元素接收 links = jQuery('a'); 如何获取带有href目的地和内容的字符串?比如: <a href="/dest1">First</a> <a href="/dest2">Second</a> need => /dest1 First, /dest2 Second 需要=> /dest1第一,dest2第二 获取第一个链接和内容 var first_link = $('a:eq(0)').a

我有一些数组,从类似的“a”元素接收

links = jQuery('a');
如何获取带有href目的地和内容的字符串?比如:

<a href="/dest1">First</a>
<a href="/dest2">Second</a>
need =>
/dest1 First, /dest2 Second

需要=>
/dest1第一,dest2第二

获取第一个链接和内容

var first_link = $('a:eq(0)').attr('href'); //first link
var first_content_text = $('a:eq(0)').text(); //content of first anchor element
var first_content_html = $('a:eq(0)').html(); //content of first anchor element including markup
要获取第二个链接和内容:

var second_link = $('a:eq(1)').attr('href'); //second link
var second_content_text = $('a:eq(1)').text(); //content of second anchor element
var second_content_html = $('a:eq(1)').html(); //content of second anchor element including markup
各种技术:

“:eq”psuedo类

$('a:eq(0)'); //this gets the first anchor. (":nth-child" pseudo-class is "0-indexed", meaning it starts counting from 0)
$("a:nth-child(1)"); //this also gets the first anchor. (":nth-child" pseudo-class is "1-indexed", meaning it starts counting from 1)
$("a").first(); //gets the first anchor element
$("a").first().next(); //gets the second anchor element
“:第n个孩子”psuedo类

$('a:eq(0)'); //this gets the first anchor. (":nth-child" pseudo-class is "0-indexed", meaning it starts counting from 0)
$("a:nth-child(1)"); //this also gets the first anchor. (":nth-child" pseudo-class is "1-indexed", meaning it starts counting from 1)
$("a").first(); //gets the first anchor element
$("a").first().next(); //gets the second anchor element
.first()和.next()方法

$('a:eq(0)'); //this gets the first anchor. (":nth-child" pseudo-class is "0-indexed", meaning it starts counting from 0)
$("a:nth-child(1)"); //this also gets the first anchor. (":nth-child" pseudo-class is "1-indexed", meaning it starts counting from 1)
$("a").first(); //gets the first anchor element
$("a").first().next(); //gets the second anchor element
获取底层DOM元素

$('a').get(0); //This gets the first element in the anchor node list
$('a')[0]; //This also does the same but cannot specify a negative index

首先获取链接和内容

var first_link = $('a:eq(0)').attr('href'); //first link
var first_content_text = $('a:eq(0)').text(); //content of first anchor element
var first_content_html = $('a:eq(0)').html(); //content of first anchor element including markup
要获取第二个链接和内容:

var second_link = $('a:eq(1)').attr('href'); //second link
var second_content_text = $('a:eq(1)').text(); //content of second anchor element
var second_content_html = $('a:eq(1)').html(); //content of second anchor element including markup
各种技术:

“:eq”psuedo类

$('a:eq(0)'); //this gets the first anchor. (":nth-child" pseudo-class is "0-indexed", meaning it starts counting from 0)
$("a:nth-child(1)"); //this also gets the first anchor. (":nth-child" pseudo-class is "1-indexed", meaning it starts counting from 1)
$("a").first(); //gets the first anchor element
$("a").first().next(); //gets the second anchor element
“:第n个孩子”psuedo类

$('a:eq(0)'); //this gets the first anchor. (":nth-child" pseudo-class is "0-indexed", meaning it starts counting from 0)
$("a:nth-child(1)"); //this also gets the first anchor. (":nth-child" pseudo-class is "1-indexed", meaning it starts counting from 1)
$("a").first(); //gets the first anchor element
$("a").first().next(); //gets the second anchor element
.first()和.next()方法

$('a:eq(0)'); //this gets the first anchor. (":nth-child" pseudo-class is "0-indexed", meaning it starts counting from 0)
$("a:nth-child(1)"); //this also gets the first anchor. (":nth-child" pseudo-class is "1-indexed", meaning it starts counting from 1)
$("a").first(); //gets the first anchor element
$("a").first().next(); //gets the second anchor element
获取底层DOM元素

$('a').get(0); //This gets the first element in the anchor node list
$('a')[0]; //This also does the same but cannot specify a negative index
您可以使用
map()
join()

演示:

  • .map()
    迭代匹配的元素
  • return
    将元素替换为返回的字符串(在对象中)
  • .get()
    将返回的jQuery对象转换为底层JS对象,在本例中是一个数组
  • .join()
    将零件连接在一起
您可以使用
map()
join()

演示:

  • .map()
    迭代匹配的元素
  • return
    将元素替换为返回的字符串(在对象中)
  • .get()
    将返回的jQuery对象转换为底层JS对象,在本例中是一个数组
  • .join()
    将零件连接在一起
试试这个:

    var hrefs_array = $.map($('a'), function(el) { return [$(el).attr("href"), $(el).text()];}) 
或者类似的事情

    var hrefs_array = [];
    $('a').each(function(index, el){
                     hrefs_array.push([$(el).attr("href"), $(el).text()]);
                });
试试这个:

    var hrefs_array = $.map($('a'), function(el) { return [$(el).attr("href"), $(el).text()];}) 
或者类似的事情

    var hrefs_array = [];
    $('a').each(function(index, el){
                     hrefs_array.push([$(el).attr("href"), $(el).text()]);
                });

我创建了一个小JSFIDLE,演示了如何做到这一点,您可以在这里查看它的实际操作:

这是完成工作的方法:

函数anathem(){
变量链接=$('a');
var anathemString=“”;
链接。每个(函数(索引){
anathemString+=$(this.attr('href')+“”+$(this.html();
如果(索引!=links.length-1){
anathemString+=“,”;
}
});
返回字符串;
}

我创建了一个小JSFIDLE,演示了如何执行此操作,您可以在此处查看它的实际操作:

这是完成工作的方法:

函数anathem(){
变量链接=$('a');
var anathemString=“”;
链接。每个(函数(索引){
anathemString+=$(this.attr('href')+“”+$(this.html();
如果(索引!=links.length-1){
anathemString+=“,”;
}
});
返回字符串;
}

也许可以看看jquery的attr和html函数。以及href属性。也许可以看看jquery的attr和html函数。在href属性。谢谢,但我有很多链接:)谢谢,但我有很多链接:)谢谢,这就是我需要的:)啊,更喜欢你的答案。我想知道将jQuery对象转换为数组的最佳方法是什么。谢谢,我只需要这些:)啊,更喜欢你的答案。我想知道将jQuery对象转换为数组的最佳方法是什么。