使用javascript函数的html文件出错
我开始体验神奇的Javascript世界,我正在制作一个带有Javascript函数和一个框的html文件,事实是警报没有显示进入文本字段的内容,我做错了什么?这是我的代码使用javascript函数的html文件出错,javascript,html,textbox,Javascript,Html,Textbox,我开始体验神奇的Javascript世界,我正在制作一个带有Javascript函数和一个框的html文件,事实是警报没有显示进入文本字段的内容,我做错了什么?这是我的代码 <html> <head> <script> function showMe() { var nombre= cnombre.value; alert("You are "+ nombr
<html>
<head>
<script>
function showMe()
{
var nombre= cnombre.value;
alert("You are "+ nombre);
cnombre.value="";
cnombre.focus();
}
</script>
</head>
<body>
Name:<input type ="text" name="cnombre"
size="30">
<input type="button" value="Go"
onClick=showMe();>
</body>
</html>
函数showMe()
{
var nombre=cnombre.value;
警惕(“你是”+nombre);
cnombre.value=“”;
cnombre.focus();
}
姓名:
您不能像那样访问文本框。有关基于代码的工作示例,请参见
请注意,添加到文本框的
id=“cnombre”
以及使用document.getElementById()
更改了您的代码,为文本框指定了一个id,然后根据该id从DOM检索文本框。这应该适用于您:
<html>
<head>
<script>
function showMe() {
var nombre = document.getElementById('cnombre');
alert("You are " + nombre.value);
nombre.value = "";
nombre.focus();
}
</script>
</head>
<body>
Name:<input type ="text" id="cnombre" name="cnombre"
size="30">
<input type="button" value="Go"
onClick=showMe();>
</body>
函数showMe(){
var nombre=document.getElementById('cnombre');
警报(“您是”+标称值);
nombre.value=“”;
nombre.focus();
}
姓名: