我想只让按钮一、二、三被点击时弹出一个对话框,但是貌似元素获取的代码不应该这么写,请高人指点下写法。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
input {
background: #CCC;
}
.active {
background: #F60;
}
</style>
<script type="text/javascript">
window.onload=function ()
{
var oNav=document.getElementById('nav');
var aInput=getByClass(oNav, 'btn')[0].getElementsByTagName('input');
var i=0;
for (i=0; i<aInput.length; i++)
{
aInput[i].onclick=function ()
{
alert('测试')
};
};
};
</script>
</head><body>
<div id="nav">
<div class="btn">
<input type="button" value="按钮一" />
<input type="button" value="按钮二" />
<input type="button" value="按钮三" />
</div>
<div class="btn">
<input type="button" value="按钮四" />
<input type="button" value="按钮五" />
<input type="button" value="按钮六" />
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
input {
background: #CCC;
}
.active {
background: #F60;
}
</style>
<script type="text/javascript">
window.onload=function ()
{
var oNav=document.getElementById('nav');
var aInput=getByClass(oNav, 'btn')[0].getElementsByTagName('input');
var i=0;
for (i=0; i<aInput.length; i++)
{
aInput[i].onclick=function ()
{
alert('测试')
};
};
};
</script>
</head><body>
<div id="nav">
<div class="btn">
<input type="button" value="按钮一" />
<input type="button" value="按钮二" />
<input type="button" value="按钮三" />
</div>
<div class="btn">
<input type="button" value="按钮四" />
<input type="button" value="按钮五" />
<input type="button" value="按钮六" />
</div>
</body>
</html>
方法要怎么写呢?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
input {
background: #CCC;
}
.active {
background: #F60;
}
</style>
<script type="text/javascript">
window.onload=function ()
{
var oNav=document.getElementById('nav');
var aInput=getByClass(oNav, 'btn')[0].getElementsByTagName('input');
var i=0;
for (i=0; i<aInput.length; i++)
{
aInput[i].onclick=function ()
{
alert('测试')
};
};
};
function getByClass(o,n){
var arr=[];
var s=o.getElementsByTagName("*");
for(var i=0;i<s.length;i++){
if(s[i].className==n){
arr.push(s[i]);
}
}
return arr;
}
</script>
</head>
<body>
<div id="nav">
<div class="btn">
<input type="button" value="按钮一" />
<input type="button" value="按钮二" />
<input type="button" value="按钮三" />
</div>
<div class="btn">
<input type="button" value="按钮四" />
<input type="button" value="按钮五" />
<input type="button" value="按钮六" />
</div>
</body>
</html>
这样试试
{
var aElm=oParent.getElementsByTagName('*');
var aArr=[];
for(var i=0; i<aElm.length; i++)
{
if(aElm[i].className==sClassName)
{
aArr.push(aElm[i]);
}
}
return aArr;
}
{
var oNav=document.getElementById('nav');
var aInput = oNav.getElementsByTagName('input');
var i=0;
for (i=0; i<3; i++)
{
aInput[i].onclick=function ()
{
alert('测试')
};
};
};
这句理解不了,可不可以麻烦你解释下,嘿嘿,刚学JS,所以……谢谢