下面的代码是面试官给出来的,他要求只可以在onload函数当中写代码实现,弹出页面当中第二个id为my的seelct对像的值,
即弹出:2,我当时给他说,这是没有办法实现的,只出那面试官看了我一眼说,你先出去吧郁闷,把这个题目发出来给大家看看能否实现。
<!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=gb2312" />
<title>无标题文档</title>
<script  language="javascript">
window.onload = function()
{
alert(document.getElementById("my").value);
}
</script>
</head>
<body>
<select name="my" id="my">
<option value="1">1</option>
</select>
<select name="my" id="my">
<option value="2">2</option>
</select>
<select name="you" id="you">
<option value="3">3</option>
</select>
</body>
</html>

解决方案 »

  1.   

    window.onload = function()
    {
        alert(document.getElementsByTagName("my")[1].value);
    }
      

  2.   


    <!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=gb2312" />
    <title>无标题文档</title>
    <script  language="javascript">
    window.onload = function()
    {
        var a = document.getElementsByName("my");
        alert(a[1].options[a[1].selectedIndex].value);
    }
    </script>
    </head>
    <body>
    <select name="my" id="my">
    <option value="1">1</option>
    </select>
    <select name="my" id="my">
    <option value="2">2</option>
    </select>
    <select name="you" id="you">
    <option value="3">3</option>
    </select>
    </body>
    </html>
      

  3.   

    一定要id的话<!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=gb2312" />
    <title>无标题文档</title>
    <script  language="javascript">
    window.onload = function()
    {
        var a = document.getElementsByTagName("select");
        var n = 0;
        for(var i=0;i<a.length;i++){
           if(a[i].id=="my"){
             n++;
             if(n==2) alert(a[i].options[a[i].selectedIndex].value);
           }
        }
    }
    </script>
    </head>
    <body>
    <select name="my" id="my">
    <option value="1">1</option>
    </select>
    <select name="my" id="my">
    <option value="2">2</option>
    </select>
    <select name="you" id="you">
    <option value="3">3</option>
    </select>
    </body>
    </html>
      

  4.   

    应该还有很多中!~
    <!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=gb2312" />
    <title></title>
    <script  language="javascript">
    window.onload = function()
    {
       alert(document.getElementsByTagName("select")[1].value);
       alert(document.getElementById("my").nextSibling.nextSibling.value);
       if(document.all)
       {alert(document.body.firstChild.nextSibling.nextSibling.value);}
       else
       {alert(document.body.firstChild.nextSibling.nextSibling.nextSibling.value)}
       alert(document.getElementById("you").previousSibling.previousSibling.value);
       alert(document.body.lastChild.previousSibling.previousSibling.previousSibling.value)
    }
    </script>
    </head>
    <body>
    <select name="my" id="my">
    <option value="1">1</option>
    </select>
    <select name="my" id="my">
    <option value="2">2</option>
    </select>
    <select name="you" id="you">
    <option value="3">3</option>
    </select>
    </body>
    </html>