【发布时间】:2012-03-14 19:21:14
【问题描述】:
我有以下 HTML 方案:
<section id="vids">
<ul>
<li>
<a data-reference="12345" href="http://www.youtube.com/watch?v=12345" rel="external" target="_blank">
<span>Video 14</span>
</a>
</li>
<!-- ... -->
</ul>
</section>
以及以下 JavaScript 部分:
$(document).ready(function() {
console.log($("#vids a")); // Returns element instance collection
$("#vids a").each(function(i, el) {
console.log(this); // Returns HTMLAnchorElement instead of the element itself
console.log(el); // Same here
console.log(i); // Returns index
});
});
我需要使用 .removeAttr() 和 .attr() 方法,但它不起作用,因为 .each() 返回元素原型而不是其实例。一个简单的 for 循环也有同样的问题。
【问题讨论】:
标签: javascript jquery for-loop element each