我想只让按钮一、二、三被点击时弹出一个对话框,但是貌似元素获取的代码不应该这么写,请高人指点下写法。
<!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>

解决方案 »

  1.   

    你的getClass方法在哪里啊?那个如果是用纯js的话得自己写
      

  2.   

    上面那个getByClass是一个写的方法?我从一个例子中看到的,我以为可以直接这么调用呢,嘿嘿
    方法要怎么写呢?
      

  3.   

    <!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('测试')
            };
        };
    };
    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>
    这样试试
      

  4.   

    方法是不是这个?这是那个例子里的代码,但是我放到我的代码中就不能用,囧……function getByClass(oParent, sClassName)
    {
        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;
    }
      

  5.   

    window.onload=function ()
    {
        var oNav=document.getElementById('nav');
        var aInput = oNav.getElementsByTagName('input');
        var i=0;
         
        for (i=0; i<3; i++)
        {
            aInput[i].onclick=function ()
            {
                alert('测试')
            };
        };
    };
      

  6.   

    arr.push(s[i]);
    这句理解不了,可不可以麻烦你解释下,嘿嘿,刚学JS,所以……谢谢