HTML文档中的Javascript未执行
问题描述:
这是来自Javascript教程的HTML文档的副本。在Chrome中打开它后,它的行为不符合预期。当我点击上面写有Set Cookie
的按钮时,无论我是否在框中写了一些东西(我们应该在页面上输出)或没有(我们应该得到一个警报),没有任何东西显示出来。为什么?HTML文档中的Javascript未执行
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
<!--
function WriteCookie()
{
if(document.myform.customer.value == ""){
alert("Enter a name");
return;
}
cookievalue = escape(document.myform.customer.value) + ";";
document.cookie="name=" + cookievalue;
document.write("Setting Cookies : " + document.cookie);
}
//-->
</script>
</head>
<body>
<form name="myform" action ="">
Enter name: <input type="text" name="customer"/>
<input type="button" value="Set Cookie" onlick="WriteCookie();"/>
</form>
</body>
</html>
答
您的输入框中有错字。它应该阅读:
<input type="button" value="Set Cookie" onClick="WriteCookie();"/>
我不想舔我的屏幕。 – epascarello
Lmao,thx!发现这些小错误是多么的困难。有没有办法从浏览器中获取某种错误信息,以使错误发现更容易? – Sahand
你可以有一个自定义的onlick属性,所以它确实不是一个错误....你可以有一个IDE插件来查看你的代码并发现无效的属性。你最好不要使用内联事件并使用addEventListener。 – epascarello