Javascript 为什么$。通过一个元素的每个循环都是正确的,但第二个循环却不正确?

Javascript 为什么$。通过一个元素的每个循环都是正确的,但第二个循环却不正确?,javascript,jquery,Javascript,Jquery,我有很多关于产品信息的行,我想获得标题、产品id和价格。当我使用$循环遍历所有项时,每个项都适用于标题和产品id,但不适用于价格。我总是得到每行的所有价格。这是我的密码: <tr id="ric685197870" class="n_ListTab1 ric_art"> <td class="n_ListPic"><img title="Das Angebot verfügt über ein Bild." alt="Das Angebot

我有很多关于产品信息的行,我想获得标题、产品id和价格。当我使用$循环遍历所有项时,每个项都适用于标题和产品id,但不适用于价格。我总是得到每行的所有价格。这是我的密码:

<tr id="ric685197870" class="n_ListTab1 ric_art">
            <td class="n_ListPic"><img title="Das Angebot verfügt über ein Bild." alt="Das Angebot verfügt über ein Bild." src="https://pics.ricardostatic.ch/ImgWeb/2/V3/listing/img.gif"></td>
            <td class="n_ListIcons"><a title="Das Angebot endet in weniger als 3 Stunden." href="javascript:help('symbol_uebersicht')"><span class="sprite icon_3h"></span></a></td>
            <td class="n_ListTitle"><a class="entry-title" href="http://www.ricardo.ch/kaufen/buecher-und-comics/belletristik/sonstige/ich-jagte-eichmann-s-wiesenthal/v/an685197870/" title="Ich jagte Eichmann/S.Wiesenthal">Ich jagte Eichmann/S.Wiesenthal</a></td>
            <td class="n_ListDate">11.9.2012 11:00</td>
            <td class="n_ListOffer">0</td>
            <td class="n_ListPrice">
            <div class="Auc">
                <span class="currency">CHF </span>0.10
            </div></td>
        </tr>
        <tr id="ric686793488" class="n_ListTab2 ric_art">
            <td class="n_ListPic"><img title="Das Angebot verfügt über ein Bild." alt="Das Angebot verfügt über ein Bild." src="https://pics.ricardostatic.ch/ImgWeb/2/V3/listing/img.gif"></td>
            <td class="n_ListIcons"><a title="Das Angebot endet in weniger als 3 Stunden." href="javascript:help('symbol_uebersicht')"><span class="sprite icon_3h"></span></a></td>
            <td class="n_ListTitle"><a class="entry-title" href="http://www.ricardo.ch/kaufen/buecher-und-comics/belletristik/romane/sonstige/angelika-overath-alle-farben-des-schnees/v/an686793488/" title="Angelika Overath: Alle Farben des Schnees">Angelika Overath: Alle Farben des Schnees</a></td>
            <td class="n_ListDate">11.9.2012 11:02</td>
            <td class="n_ListOffer">1</td>
            <td class="n_ListPrice">
               <div class="Auc">
                  <span class="currency">CHF </span>4.00
               </div></td>
</tr>

<script type="text/javascript">
$.each($(".n_ListTitle"), function(i, v) {
            var node = $(this);
            var nodeParent = node.parent();
            var nodeText = node.text();
            var nodePrice = nodeParent.children(i)[5];

            var prodPrice = $(nodePrice.nodeName + ' div').text();
            var prodId = nodeParent.attr('id').replace('ric', '');
            var prodTitle = nodeText;

            var json = {
                id : prodId,
                price : prodPrice,
                currency : "CHF",
                name : prodTitle
            };
            console.log(json);
        });

11.9.2012 11:00
0
瑞士法郎0.10
11.9.2012 11:02
1.
瑞士法郎4.00
$.each($(“.n_ListTitle”),函数(i,v){
var节点=$(此节点);
var nodeParent=node.parent();
var nodeText=node.text();
var nodePrice=nodeParent.children(i)[5];
var prodPrice=$(nodePrice.nodeName+'div').text();
var prodId=nodeParent.attr('id')。replace('ric','');
var prodTitle=nodeText;
var json={
id:prodId,
价格:prodPrice,
货币:“瑞士法郎”,
姓名:prodTitle
};
log(json);
});

我明白了:

对象
货币:“瑞士法郎”
id:“681625138”
名称:“贝弗利·哈珀:施瓦泽·纳赫特的海勒·蒙德——塔申布赫”
价格:问题是:每一行的每一个价格

我希望这是可以理解的,我希望有人能帮我弄明白。谢谢

这适用于数组():

您需要():

这是针对数组()的:

您需要():

这和你想象的不一样
nodePrice.nodeName
“tr”
,因此您的选择器是:

$('tr div')
因此,您得到了所有的价格,因为您选择了
tr
的子级
div

你真正想要的是:

var prodPrice = $('div', nodePrice).text();
这相当于
$(nodePrice).find('div')

此外:

你为什么在这里传递
i
i
是“数组”中的索引,它对
.children
没有任何用处。您要使用以下命令:

var nodePrice = nodeParent.children().eq(5);
或者更好(你有这些类,让我们使用它们):

这和你想象的不一样
nodePrice.nodeName
“tr”
,因此您的选择器是:

$('tr div')
因此,您得到了所有的价格,因为您选择了
tr
的子级
div

你真正想要的是:

var prodPrice = $('div', nodePrice).text();
这相当于
$(nodePrice).find('div')

此外:

你为什么在这里传递
i
i
是“数组”中的索引,它对
.children
没有任何用处。您要使用以下命令:

var nodePrice = nodeParent.children().eq(5);
或者更好(你有这些类,让我们使用它们):

试试这个:

var nodePrice = node.siblings(".n_ListPrice");
然后,您可以使用

$(nodePrice).text();
试试这个:

var nodePrice = node.siblings(".n_ListPrice");
然后,您可以使用

$(nodePrice).text();
宁愿使用另一个选择器而不是
eq(5)
如果您决定稍后添加一行,您将不得不更改所有这些。。。使用
find()
查找带有priceholder类的TD,并选择其中的div。好了:)


宁愿使用另一个选择器而不是
eq(5)
如果您决定稍后添加一行,您将不得不更改所有这些。。。使用
find()
查找带有priceholder类的TD,并选择其中的div。好了:)

不,我还是不明白你所说的“每一行的每一个价格”你的问题是这一行
nodeParent.children(I)[5]
没有任何意义
i
是一个dom元素,子元素需要一个选择器<代码>[5]也没有什么意义。@KevinB:
i
实际上是“数组”中的索引。不,我还是不明白你所说的“每一行的每一个价格”是什么意思。你的问题是这行
nodeParent.children(i)[5]
没有任何意义
i
是一个dom元素,子元素需要一个选择器
[5]
也没有什么意义。@KevinB:
i
实际上是“数组”中的索引。选择器的结果是数组。
$。每个
都能很好地处理jQuery对象。@TimRogers选择器的结果是类似数组的对象,而不是数组。:)选择器的结果是一个数组。
$。每个
都能很好地处理一个jQuery对象。@TimRogers选择器的结果是一个类似数组的对象,而不是数组。:)谢谢你的解释!这是可以理解的,而且现在效果很好:)@Marcciarfaglia确保你将他的答案标记为已接受,这对你和回答你问题的人都有帮助。非常感谢你的解释!这是可以理解的,而且现在效果很好:)@Marcciarfaglia确保你将他的答案标记为已接受,这对你和回答你问题的人都有帮助。
var nodePrice = nodeParent.find('.n_ListPrice div');
var prodPrice = nodePrice.text();