Javascript 为什么可以';t我获取.uniqueId()来处理同一类的所有元素

Javascript 为什么可以';t我获取.uniqueId()来处理同一类的所有元素,javascript,jquery,jquery-ui,Javascript,Jquery,Jquery Ui,我有一个元素列表。为什么我不能使用uniqueId()?以下是我所拥有的: <a href="#" class="anchor">This</a> <a href="#" class="anchor">This</a> <a href="#" class="anchor">This</a> <a href="#" class="anchor">This</a> 我也尝试过: this.uniqueI

我有一个
元素列表。为什么我不能使用
uniqueId()
?以下是我所拥有的:

<a href="#" class="anchor">This</a>
<a href="#" class="anchor">This</a>
<a href="#" class="anchor">This</a>
<a href="#" class="anchor">This</a>
我也尝试过:

this.uniqueId();
提前感谢。

只需使用:

$('.anchor').uniqueId();
演示:

$('.anchor').uniqueId();
$('.anchor')。每个(函数(){
console.log($(this.attr('id'));
});


uniqueId()
是一个jQueryUI方法,那么您是否包括了jQueryUI库以及jQuery本身?检查控制台是否有错误。虽然我建议,如果你正确地构造你的HTML,你根本不需要自动生成一个
id
。我用jQueryUI测试了它。它工作得很好。@zfrisch更进一步,您不需要
each()
:@rorymcrossan您是对的,但它直接来自OP。只是想证明它工作得很好。:)