日期:2014-05-16 浏览次数:20434 次
num[0] = 1; num[1] = 3; num[2] = 2; num[3] = 4; num[4] = 13; num[5] = 51;
<a href="#" id="a0"></a> <a href="#" id="a1"></a> <a href="#" id="a2"></a> <a href="#" id="a3"></a> <a href="#" id="a4"></a> <a href="#" id="a5"></a>
$("#a0").click(function(){myFunction(num[0])});
$("#a5").click(function(){myFunction(num[5])});
for(var i = 0; i< num.length; i++){
$("a" + i).click(function(){ myFunction(num[i]); }); //实际上这个代码有问题
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<script>
var arr = [
'测试1',
'测试2',
'测试3',
'测试4',
'测试5',
'测试6'
];
for(var i = 0; i < arr.length; i++){
var a = document.createElement('a');
a.innerHTML = arr[i];
a.href = '#';
(function(i){
a.onclick = function(){
return myFunction(i)
}
})(i);
document.body.appendChild(a);
document.write('<br/>')
}
function myFunction(i){
alert(i)
}
</script>
</body>
</html>
------解决方案--------------------
$("a").each(function(i){
$(this).bind("click",{MenuIndex:i},function(event){
myFunction(num[event.data.MenuIndex]);
})
});
------解决方案--------------------
<html>
<head>
<title></title>
<script type="text/javascript">
window.onload = function () {
var aLinkListElement = document.getElementById("aLinkList");
aLinkListElement.onclick = function (e) {
e = e || window.event;
var srcElement = e.target || e.srcElement;
var elementName = srcElement && srcElement.nodeName;
if (!elementName) {
return;
}
if (elementName.toUpperCase() != "A") {
return;
}
aOnclick(srcElement.getAttribute("value"));
}
}
function aOnclick(value) {
alert(value);
}
</script>
</head>
<body>
<div id="aLinkList">
<a h