我正在关注W3Schools网站上的JavaScript教程,我有以下代码:
<html>
<head>
<title>Hello!</title>
</head>
<body>
<script type="text/javascript">
function confirmShow
{
var r = confirm("Press one...")
if (r == true)
{
alert("Button pressed == OK")
}
if (r == false)
{
alert("Button pressed == Cancel")
}
}
</script>
<input type="button" onclick="confirmShow()" value="Show Confirm Box" />
</body>
</html>
每当我在Coda或Safari中预览时,警报都不会出现。
提前致谢!
答案 0 :(得分:3)
“function confirmShow”=&gt; “function confirmShow()”
Firebug适合js调试,试试吧。 Safari也有选项,AFAIK。
答案 1 :(得分:1)
功能确认显示 {
功能confirmShow() { ?
答案 2 :(得分:0)
我不知道这是不是您的问题,但您的按钮 <body>
标记。这可能会给你带来一些麻烦......
通常也会在<head>
元素中放置这样的脚本。仅供参考。
答案 3 :(得分:0)
1)w3schools充满了错误和遗漏。可以在howtocreate.co.uk找到更好的教程
2)您没有DOCTYPE声明,并且您正在使用XHTML语法。
2.1)IE不支持true,请参阅webdevout.net/articles/beware-of-xhtml以获取更多信息 3)您需要根据规范
封装元素和另一个块级元素请参阅下面的正确HTML5文档。注意位置和语法
<!DOCTYPE html>
<html>
<head>
<title>Hello!</title>
<script>
function confirmBox() {
var ret = confirm('Some Text');
/*
Note the 3 equal signs. This is a strict comparison operator, to check both the 'value' as well as the type. see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators for more
*/
if(ret === true) {
alert('Alert box for "Okay" value');
}
else if(ret === false) {
alert('Alert box for "Cancel" value');
}
}
window.onload = function() {
// Execute the confirmBox function once the 'button' is pressed.
document.getElementById('confirmBox').onclick = confirmBox;
}
</script>
</head>
<body>
<form>
<p>
<input type="button" id='confirmBox' value="Show Confirm Box">
</p>
</form>
</body>
</html>