Last active
December 16, 2015 11:49
-
-
Save cssoul/5430148 to your computer and use it in GitHub Desktop.
遍历元素, 点击获取索引值
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--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