假设我在页面上有产品链接,一些产品在推荐 div 中:
<a href="some_url?pid=abc"><img src ... ></a>
<a href="some_url?pid=abc">ABC Product Name...</a>
<a href="some_url?pid=def">
.... more product links
<div class="recommendations">
<a href="some_url?pid=uvw"> ...
<a href="some_url?pid=xyz"> ...
</div>
我需要为推荐和不推荐的 url 构建唯一的 pid 列表,如下所示:
recommended_pids = ["uvw","xyz"];
non_recommened_pids = ["abc","def"];
我知道我可以在这样的页面上获取所有 pid 的列表:
$('a[href*="pid="]').each(function() {
//get just the pid part
var link = this.href;
var pid = link.split('pid=')[1].split('&')[0];
pids.push(pid);
});
我可以使用这样的选择器在页面上获取推荐的 pid 列表:
$('div.recommended a[href*="pid="]')
对每个数组进行排序和 uniq,然后减去所有元素,然后进行数组减法以获得不推荐的 pid 列表。
但是有没有办法使用其他 jQuery 过滤器来获取不包含在推荐 div 中的 pid 列表,而无需编写数组减法函数?