JavaScript的函数返回值无效...
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript的函数返回值无效...相关的知识,希望对你有一定的参考价值。
是教材上的代码,如下。
<html>
<head>
<title>Two Functions Program</title>
<script language="javascript">
<!--hide from incompatible browsers
function print_message(first_message)
document.writeln(first_message);
function return_message(second_message)
return "This message was returned from a function";
//stop hiding from incompatible browsers-->
</script>
</head>
<body>
<pre>
<script language="javascript">
<!--hide from incompatible browsers
var return_value=return_message();
print_message("This message was printed from a function");
//stop hiding from incompatible browsers-->
</script>
</pre>
</body>
</html>
已经核对过,跟书本一模一样了。可是在ie上面看的话,只能够显示结果为“This message was printed from a function”。按书本的结果,应该是还有一句“This message was returned from a function”才对的。
请问这是什么问题呢?
不过我刚遇到同样的问题,如下。
<html>
<head>
<title>green page</title>
<script language="JavaScript">
</head>
<!--hide from incompatible browsers
function confirmpagechange()
return confirm
("Are you sure you want to dispalay the red page?");
//stop hiding from incompatible browsers-->
</script>
</head>
<body bgcolor="green">
<a href="redpage.html"
onclick="return confirmpagechange();"
onmouseover="status='This link opens the red page!';return false;"
onmouseout="status='YOu did not open the red page!!';return true;">
click here to open the green page</a>
</body>
</html>
怎么点击了链接的时候,没有弹出confirm对话框要求确定呢?是那个confirmpagechange函数的问题吗?如果我不用函数,直接把confirm写在onclick里面就可以了。为什么?
print_message("This message was printed from a function");
改一下:
print_message("This message was printed from a function"+return_value);
解释:return_message函数成功返回了值,但例子当中只是将这个值保存在一个变量当中,并没有显示出来,因此就看不到该有的效果了。
有问题欢迎找我讨论
问题2:有两个错误
第一个是第5行的</head>删掉;
第二个是confirmpagechange函数的问题,里面的内容要写在一行上,这样:
return confirm("Are you sure you want to dispalay the red page?"); 参考技术A var return_value=return_message();
你现在虽然调用了这个方法,得到了
return "This message was returned from a function";
返回结果,
但是你并没有输出它
document.write(return_value); 参考技术B var return_value=return_message();没有输出的 参考技术C <html>
<head>
<title>Two Functions Program</title>
<script language="javascript">
<!--hide from incompatible browsers
function print_message(first_message)
document.writeln(first_message);
function return_message(second_message)
return "This message was returned from a function";
//stop hiding from incompatible browsers-->
</script>
</head>
<body>
<pre>
<script language="javascript">
<!--hide from incompatible browsers
var return_value=return_message();
document.writeln(return_value);
print_message("This message was printed from a function");
//stop hiding from incompatible browsers-->
</script>
</pre>
</body>
</html>
跟得真快啊....。呵呵
以上是关于JavaScript的函数返回值无效...的主要内容,如果未能解决你的问题,请参考以下文章