用一个例子说明一下:
<html>
<body>
<a href="https://wwww.baidu.com" onclick="return add_onclick()">Open</a>
<input type="text" id="id_test" value="fuck">
<script>
function add_onclick(){
//其他代码
window.location.href='https://www.bilibili.com';
var myelem = document.getElementById("id_test").value
alert(myelem);
return false;
}//or false;})
</script>
</body>
</html>>
JavaScript在事件中调用函数时用return返回值实际上是对window.event.returnvalue进行设置。**
- 返回true时,点击链接,会执行链接标签的href链接,即跳转到百度,而不会执行window.location.href的跳转操作,而且也会弹出alert。
- 返回false时,点击链接,不会执行链接标签的href链接,即不会跳转到百度,而会执行window.location.href的跳转操作,而且也会弹出alert
通过这个例子说明:
当事件返回true时,会执行链接本身的跳转操作,而忽略事件处理函数中的window.location.hre跳转操作。
当事件返回false时,会终止链接本身的跳转操作,而执行事件处理函数中的window.location.hre跳转操作。