我正在调用一个名为'capital()'的函数,但它不起作用,而'人均'正在运行.它是js中的关键字还是这个?
这是代码看到它,测试它
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML Select Element</title>
<script type="text/javascript">
function capita(){
alert('yes');
c = new Array("Islamabad", "Tehran", "Bejing", "New Delhi", "Kabul");
var i;
i = document.f1.country.selectedIndex;
document.f1.capital.value=c[i];
}
</script>
</head>
<body>
<h3>Countries and Capital</h3>
<form name="f1" method="post">
The capital of
<select name="country" id="country" onChange="capita();">
<option selected>Pakistan
<option>Iran
<option>China
<option>India
<option>Afghanistan
</select>
is <input type="text" name="capital" value="Islamabad"/>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
capital 是对input元素的引用.
<input type="text" name="capital" value="Islamabad"/>
Run Code Online (Sandbox Code Playgroud)
这是因为当您使用内联处理程序时,某些DOM元素会插入到变量作用域链中.究竟要插入哪些元素在某种程度上取决于浏览器.
如果onchange将select元素的处理程序更改为:
<select name="country" id="country" onChange="alert(capital);">
Run Code Online (Sandbox Code Playgroud)
你会看到它警告:
"[object HTMLInputElement]"
Run Code Online (Sandbox Code Playgroud)
......或类似的东西.
这是一个更新内联处理程序的更新示例,如下所示:
<select name="country" id="country" onChange="alert('LOCAL: ' + capital +
'\n\nGLOBAL: ' + window.capital);">
Run Code Online (Sandbox Code Playgroud)
所以现在它提醒capital和window.capital.如果您在不同的浏览器中测试它,您可能会得到不同的结果.
Chrome显示:
LOCAL:[对象HTMLInputElement]
GLOBAL:function capital(){}
| 归档时间: |
|
| 查看次数: |
175 次 |
| 最近记录: |