jQuery选择器:罕见的用例 [英] jquery selectors: an uncommon use case
问题描述
我必须解析以这种方式组织的html页面:
I have to parse an html page organized this way:
<li id="list">
<a id="cities">Cities</a>
<ul>
<li>
<a class="region" title="liguria">Liguria</a>
<ul>
<li>
<a class="genova">Genova</a>
</li>
<li>
<a class="savona">Savona</a>
</li>
</ul>
</li>
<li>
<a class="region" title="lazio">Lazio</a>
<ul>
<li>
<a class="roma">Roma</a>
</li>
</ul>
</li>
</ul>
</li>
我需要提取所有城市的列表.我不在乎地区... 我正在使用来自 node.js 的 cheerio ,但是由于 cheerio 使用了jquery样式选择器(AFAIK ...),因此我向标签添加了jquery.
I need to extract a list of all the cities. I don't care about regions... I am using cheerio from node.js, but I added jquery to the tags since cheerio uses jquery-style selector (AFAIK...).
我提供了部分解决方案,部分解决了问题(它只列出了第一批地区城市...):
I have come with this partial solution, partially working (it only lists first region group cities...):
$('li[id="list"] li li').each(function(i, elem) {
console.log('city:', elem.children[0].next.children[0].data);
});
如您所见,我很困惑... :-(
有任何线索吗?
As you can see, I'm quite confused... :-(
Any clue?
推荐答案
尝试:
$('li#list ul li ul li a').each(function() {
console.error("City: "+$(this).html());
});
Try:
$('li#list ul li ul li a').each(function() {
console.error("City: "+$(this).html());
});
如下所述,选择器可以简单地是$('li#list li li a')
.
As noted below, the selector could be simply $('li#list li li a')
.
这篇关于jQuery选择器:罕见的用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!