<html>
<head>
<script>
function calledHere(value) { console.log(value); }
</script>
</head>
<body>
<form name="test">
<input type="button" value="click!" onclick="calledHere(test);" >
</form>
</body>
</html>
从上面的代码中,在onclick调用中错误地传递了没有引号的表单名称。但是,输出显示整个表单DOM元素。不知道它是如何得到表单元素的。
任何帮助对我在javascript方面的学习都会更有用。
提前致谢!!!
答案 0 :(得分:0)
您已经在您的“价值”中获得了表单元素。变量。您查看元素记录的原因是因为console.log在元素上调用了.toString()方法。
此代码演示了&#39;值&#39;引用表单元素:
<html>
<head>
<script>
function calledHere(value) { value.style.backgroundColor = "red" }
</script>
</head>
<body>
<form name="test">
<input type="button" value="click!" onclick="calledHere(test);" >
</form>
</body>
</html>
http://jsfiddle.net/John_C/KZ3KR/
这个答案解释了变量的范围;为什么你可以将表单名称传递给函数:https://stackoverflow.com/a/1416157/1588990