jQuery find()只返回第一个匹配的结果? [英] jQuery find() returns only the first matched result ?

查看:1062
本文介绍了jQuery find()只返回第一个匹配的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jQuery中使用$ .find()方法,我无法获得所有匹配选择器条件的结果。



这是我的HTML

 < div class =something > 
< label>你好< / label>
< div class =selse>
< label>嗨< / label>
< label class =imp>这是< / label>
< label class =imp> Nooo< / label>
< / div>
< label class =imp>斯巴达< /标签>
< label class =imp>对 ? < /标签>
< / div>

< div class =something>
< label>地狱没有< /标签>
< div class =selse>
< label>嗨< / label>
< label class =imp>不可以< / label>
< / div>
< label class =imp>触摸< / label>
< label class =imp>这< /标签>
< label class =imp> MC< / label>


所以当我做下面的JS


$ b $

  $(div.something)。each(function(index){
alert(index +':'+ $这个).find(label.imp)。html())
});



我预料它会给我2个警报。一个 0。这是,Nooo,Sparta,对吗?,另一个是 1。不能,碰,这个,MC 。但是我只是 0。这是 1。不能



我试过在像这样的函数中使用数组

  $ (div.something)。each(function(index){
var arr = [] $ b $ arr = $(this).find(label.cidForm)。html();
alert(arr);
});

未定义在其中。我在这两种情况下做错了什么?我只想要一个包含 label.imp 元素内所有值的数组。

这里有一个JSFiddle我也是一样的。 http://jsfiddle.net/WPeKF/1/

解决方案

.html()和其他getter方法只返回第一个匹配元素的值。考虑到这一点,我认为你可以找出需要做出的逻辑改变。

小提琴: http://jsfiddle.net/WPeKF/2/



代码:


$ b $ ($)$(this).find(label.imp)。 map(function(){
return $(this).html();
)。get()。join();
))。get();
console.log(arr);


I am using the $.find() method in jQuery and i am not able to get all the results which match the selector condition.

This is my HTML

<div class="something">
<label> Hello </label>
<div class="selse">
    <label> Hi </label>
    <label class="imp"> This is </label>
    <label class="imp"> Nooo </label>
</div>
<label class="imp"> Sparta </label>
<label class="imp"> Right ? </label>
</div>

<div class="something">
<label> Hell No </label>
<div class="selse">
    <label> Hi </label>
    <label class="imp"> Cant </label>
</div>
<label class="imp"> touch </label>
<label class="imp"> this </label>
 <label class="imp"> MC  </label>
</div>​

So when i do the following JS

$("div.something").each(function(index) {
   alert(index + ': ' + $(this).find("label.imp").html())
    });​

I expected that it'll give me 2 alerts . One with 0. This is, Nooo, Sparta, Right ? and the other with 1. Cant, touch, this, MC . But i got just 0. This is and 1. Cant .

I tried using arrays in the same function like this

$("div.something").each(function(index) {
    var arr=[]
    arr = $(this).find("label.cidForm").html();
    alert(arr);
    });​

No i get alert boxes with 'Undefined' in them. What am i doing wrong in both these cases ? I just want an array with all the values inside label.imp elements.

Here's a JSFiddle i put up for the same. http://jsfiddle.net/WPeKF/1/

解决方案

.html() and other getter methods only return the value of the first matched element. With that in mind, i think you can figure out the logic changes that need to be made.

Fiddle: http://jsfiddle.net/WPeKF/2/

Code:

var arr = $("div.something").map(function(){
    return $(this).find("label.imp").map(function(){
        return $(this).html();
    }).get().join("");        
}).get();
console.log(arr);

这篇关于jQuery find()只返回第一个匹配的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆