我有很多包含产品信息的行,我想获取标题、产品 ID 和价格。当我用 $.each 遍历所有内容时,它确实适用于标题和产品 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);
});
我明白了:
Object
currency: "CHF"
id: "681625138"
name: "BEVERLEY HARPER: HELLER MOND IN SCHWARZER NACHT--TASCHENBUCH"
price: " here is the problem: every price in every row "
我希望它是可以理解的,我希望有人能帮我弄清楚。谢谢!
最佳答案
var prodPrice = $(nodePrice.nodeName + ' div').text();
这并不像您想象的那样。 nodePrice.nodeName
是 "tr"
,所以您的选择器是:
$('tr div')
因此,您将获得所有价格,因为您正在选择作为 tr
子元素的每个 div
。
你真正想要的是:
var prodPrice = $('div', nodePrice).text();
这相当于$(nodePrice).find('div')
还有:
var nodePrice = nodeParent.children(i)[5];
为什么要在这里传递 i
? i
是“数组”中的索引,它对.children
没有任何用处。你想使用这个:
var nodePrice = nodeParent.children().eq(5);
或者,更好的是(你有这些类,让我们使用它们):
var nodePrice = node.siblings('.n_ListPrice');
关于javascript - 为什么 $.each 循环遍历一个元素是正确的,但对于第二个元素却不是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12585255/