1、首先,新建一个html5文件,输入初始化代码。
2、新建一个h1标签,用于接收到时js中function函数的返回值。
3、新建一个script标签,在标签里面新建一个function函数,函数返回值为22。
4、在h1标签中也新建一个script标签,标签中新建一个str变量接收上面函数aa的返回值。
5、通过“document.write(变量)”的方法将接收返回值的变量打印出来。
6、最后我们用浏览器打开index.html文件,查看页面效果,这样我们就获取到js中function返回值了。
1.语法及返回方式①返回控制与函数结果
语法为:return 表达式
语句结果函数的执行,返回调用函数,而且把表达式的值作为函数结果返回出去
②返回控制无函数结果
语法为:return
在大多数情况下,为事件处理函数如果让其返回false,可以防止默认的事件行为.例如,默认情况下,点击一个<a>标签元素,页面会跳转到该元素href属性指定的页. 而return false就相当于终止符,return true就相当于执行符. 在js中return false的作用一般是用来取消默认动作的.比如你单击一个链接除了触发你的"onclick"事件以外还要触发一个默认事件就是执行页面的跳转.所以这时候如果你想取消对象的默认动作就可以return false来阻止它的动作.也就是说如果你想用js代码来局部改变一些数据而不引起页面其他部位的变化,那么你就应该在onclick事件代码的后面加上return false
在js中,通常用return false来阻止提交表单或者继续执行下面的代码,通俗来说就是阻止执行默认的行为.
常用两种返回方法:1.window.showModalDialog(url,args,dialogattrs)
参数说明:
url:弹出页面地址
agrs:主窗口传给对话框的参数,可以是任意类型(数组也可以)
dialogattrs:弹出窗口的样式参数
模式对话框用法:
主窗口:var value =window.showModalDialog('test.jsp',strs,'resizable:yes')
弹出框中通过window.returnValue设置返回值。
2。window.open:
【父窗口】
代码如下:
<script>
function show_child()
{
var child=window .open("child.html","child","height=200,width=400,status=yes,toolbar=no,menubar=no,location=no")
/* if(!child.closed)
{
if(!window .close())
{
var textValue = frm.txt.value parent.frm0.txt0.value = textValue
}
else
{
window .close()
child.close()
}
}*/
}
</script>
<a href="javascript:show_child()">打开子窗口</a>
<form name=frm0>
<input type="text" name="txt0" id="txt0"> //注意这里一定要写ID属性不然FF下取不到值
</form>
【子窗口】
代码如下:
<script>
function choseItem()
{
var v=""
var check_item = document.frm.item
for(i=0i<check_item.lengthi++)
{
if(check_item[i].checked)
{
v+=","+check_item[i].value
}
document.frm.txt.value=v.replace(/^,{1}/,"")
}
}
function foo()
{
window .close()
window .opener.document.getElementById("txt0").value=document.getElementById("txt").value
}
</script>
<body>
<form name=frm>
<input type=checkbox name=item value=1 onclick="choseItem()">a
<input type=checkbox name=item value=2 onclick="choseItem()">b
<input type=checkbox name=item value=3 onclick="choseItem()">c
<input type=checkbox name=item value=4 onclick="choseItem()">d
<input type=text name="txt" id="txt">
</form>
<input type=button value="关闭" onclick="foo()">
</body>