Skip to content

Instantly share code, notes, and snippets.

@cssoul
Last active December 16, 2015 11:49
Show Gist options
  • Save cssoul/5430148 to your computer and use it in GitHub Desktop.
Save cssoul/5430148 to your computer and use it in GitHub Desktop.
遍历元素, 点击获取索引值
<!--HTML-->
<ul id="J_Item">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
</ul>
<!--JavaScript-->
<script>
var items = document.getElementById("J_Item").getElementsByTagName("a");
for(var i=0; i<items.length; i++){
items[i]['index'] = i;//把索引值赋值给对象的 index 属性
items[i].onclick = A;
}
function A(){
alert(this['index']);//alert索引值
return false;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment