我正在试着做这样的事情:
var items = $("main a").unique();
var links = [];
items.each(function (index, value)
{
links.push({
href: this.href,
text: this.text
});
});其思想是找到页面上的所有链接并删除重复项,生成一个具有href/text对的对象。但是上面的方法不起作用,我得到的是"TypeError:$(...).unique is not a function“
发布于 2018-01-26 09:31:48
您错误地调用了$.unique()。它不是jQuery对象的方法,而是一个实际的函数。你真的想要:
$.uniqueSort($("main a"))
此外,学习使用.map()而不是推送数组:
var links = $.uniqueSort($("main a")).map(function(idx, e) {
return {href: e.href, text: e.text}
})(注意:此.map()是jQuery版本,其中回调的第一个参数是数组/对象的索引,而不是数组元素,因为应用于jQuery对象的$.uniqueSort()将返回jQuery对象,而不是本机JavaScript数组)。
发布于 2018-01-26 09:30:44
https://stackoverflow.com/questions/48454564
复制相似问题