$.makeArray
返回一个原生JavaScript阵列,而不是一个jQuery对象。原生JavaScript数组没有像.attr()
这样的jQuery方法。这开始有意义吗?
传递videoLinks
到$.makeArray
根本没有任何意义,因为你要么传递函数videoLinks
,或者功能本地videoLinks
这是已经一个jQuery对象。所以,我认为这是沿着你想要做什么线条更:
function videoLinks() {
$("a[id^=a_l_]").each(function() {
console.log(this.href);
});
}
这将记录每一个<a>
元素的href
属性与id
与'a_l_'
开始。也许你想要建立一组这样的属性,而不是记录它们。然后你会使用.map()
和.get()
:
function videoLinks() {
var hrefs = $("a[id^=a_l_]").map(function() {
return this.href;
}).get(); // ← note the .get() call
}
我的终极目标是返回一个链接随机
然后你几乎没有。 Just get a random element from the hrefs
array:
function videoLinks() {
var hrefs = $("a[id^=a_l_]").map(function() {
return this.href;
}).get(); // ← note the .get() call
var randHref = hrefs[Math.floor(Math.random() * hrefs.length)];
console.log(randHref);
}
哇这么简单:)嗯,这正是我想要的目的。非常感谢你。 – 2012-04-17 01:13:53
好的我的最终目标是随机返回一个链接 – 2012-04-17 01:16:16
现在,你为什么不首先这么说? ':)'编辑。 – 2012-04-17 01:19:49