这是我的代码:
$.each($('.pages a[href!="#"]'), function (idx, elem) {
var href = $(this).attr('href')
// other code
})
基本上,我正在使用JQuery的.each
来遍历所选元素,但我正在使用$(this)
来访问每个元素的属性。 JSHint抱怨这一点,说elem
和idx
已定义但从未使用过。
还有另一种方法可以解决这个问题吗?
答案 0 :(得分:3)
如果不使用它们,则根本不需要包含它们:
$.each($('.pages a[href!="#"]'), function () {
var href = $(this).attr('href')
// other code
})
请注意,each()
在元素集合上使用时的最佳做法是直接在jQuery对象上使用它:
$('.pages a[href!="#"]').each(function() {
var href = $(this).attr('href')
// other code
})