遍历元素 - 如何避免“(var)已定义但未使用”错误?
Looping through elements - how to avoid "(var) is defined but not used" errors?
这是我的代码:
$.each($('.pages a[href!="#"]'), function (idx, elem) {
var href = $(this).attr('href')
// other code
})
基本上,我使用 JQuery 的 .each
来遍历所选元素,但我使用 $(this)
来访问每个元素的属性。 JSHint 抱怨这个,说 elem
和 idx
被定义但从未使用过。
是否有另一种方法可以避免 运行 出现此类错误?
如果不使用它们,您根本不必包括它们:
$.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
})
这是我的代码:
$.each($('.pages a[href!="#"]'), function (idx, elem) {
var href = $(this).attr('href')
// other code
})
基本上,我使用 JQuery 的 .each
来遍历所选元素,但我使用 $(this)
来访问每个元素的属性。 JSHint 抱怨这个,说 elem
和 idx
被定义但从未使用过。
是否有另一种方法可以避免 运行 出现此类错误?
如果不使用它们,您根本不必包括它们:
$.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
})