下面的代码是面试官给出来的,他要求只可以在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>
即弹出: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>
解决方案 »
- 帮我解释一下 这段JQ
- js 画图,100分汇报,通过立即结贴。
- js一个函数中如何调用另一个函数中的实例函数?
- 获得图片的大小
- [讨论]关于javascript中for语句中变量的作用域
- 下拉列表的问题
- 怎么样把变量传到用 window.showModelessDialog 打开的窗口???
- 高分在线求助:如何让下面"邮件正在发送, 请稍候..."框在当前页面经过一段时间自动消失??
- 窗口背景问题
- jquery 获取 json数据里的日期 yyy-mm-dd {"nanos":0,"time":1413903530000,"minutes":58,"sec
- document.getelementbyid 和$()疑问
- 急!!!!!!JavaScript
{
alert(document.getElementsByTagName("my")[1].value);
}
<!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>
<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>
<!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>