如何使.each函数一次输出一个HTML元素匹配而不是所有匹配

时间:2014-07-07 23:37:44

标签: jquery

这个jQuery .each函数返回所有匹配元素的文本,而不是一次只返回一个。

澄清:两个文本元素嵌套在每个$review

  var reviews = function(error, result, $) {
    $review = $(".ZWa.nAa");

  $review.each(function(index, a){
    $name = $('.d-s.ob.tv.Ub.ZUb.M5').text();
    $comment = $('.GKa.oAa').text();

    console.log("====" + $name + "====");
    console.log($comment);
  });
};

这是当前的输出:

====Tiffany NelsonKristin Ruiz====
I had never been to a chiropractor before and was not thrilled at the idea of going, but my back was killing me and I had no other choice. Dr Rosquist and his staff helped to ease my fear, kept me informed of everything they were doing and why, and were so helpful and friendly. After one visit they remembered who I was and even now when I go in only once a month or so they know me by name and ask me questions about my personal life and remember details from previous discussions. 

My back is doing much better and I feel like I can go back to my active lifestyle without being afraid of hurting my back again. They also helped me solve some other problems I had been having through some blood work tests and were helpful to explain why and why was going on. 

====Tiffany NelsonKristin Ruiz====
I had never been to a chiropractor before and was not thrilled at the idea of going, but my back was killing me and I had no other choice. Dr Rosquist and his staff helped to ease my fear, kept me informed of everything they were doing and why, and were so helpful and friendly. After one visit they remembered who I was and even now when I go in only once a month or so they know me by name and ask me questions about my personal life and remember details from previous discussions. 

My back is doing much better and I feel like I can go back to my active lifestyle without being afraid of hurting my back again. They also helped me solve some other problems I had been having through some blood work tests and were helpful to explain why and why was going on. 

我想要回归的是:

====Tiffany Nelson====
I had never been to a chiropractor before and was not thrilled at the idea of going, but my back was killing me and I had no other choice. Dr Rosquist and his staff helped to ease my fear, kept me informed of everything they were doing and why, and were so helpful and friendly. After one visit they remembered who I was and even now when I go in only once a month or so they know me by name and ask me questions about my personal life and remember details from previous discussions. 

====Kristin Ruiz====
My back is doing much better and I feel like I can go back to my active lifestyle without being afraid of hurting my back again. They also helped me solve some other problems I had been having through some blood work tests and were helpful to explain why and why was going on. 

如何获得此输出?

1 个答案:

答案 0 :(得分:1)

如果没有看到你的HTML,我会假设2个文本元素嵌套在每个$review

在这种情况下,您希望使用$review作为实例查看this的特定实例,并find()遍历它的后代

  $review.each(function(index, a){
    $name =$(this).find('.d-s.ob.tv.Ub.ZUb.M5').text();
    $comment = $(this).find('.GKa.oAa').text();

    console.log("====" + $name + "====");
    console.log($comment);
  });

如果这不是当前的结构,请提供一些HTML。

您正在做的是查看每个课程页面中的整个集合。任何getter方法只能拉取它找到的第一个值,因此无论集合中有多少元素,它都会返回相同的值