1

来自 jquery 的这个简单代码没有获取卸载事件。控制台说缓存被禁用。

我希望用户在点击我的页面上离开页面的每个链接时得到提示,但在他们关闭页面时不提示。有什么建议么。

<html>

<script src="jquery-3.1.1.min.js"></script>

<body>

<a href="https://www.science.gov" id="navigate">science</a>

</body>

<script>

    $(window).on('beforeunload', function(){
    return('Are you sure you want to leave?')};

</script>

</html>
4

2 回答 2

1

您的代码中有错误,最后您错过了 )

Uncaught SyntaxError: missing ) 在参数列表之后

将代码更改为: $(window).on('beforeunload', function(){ return('Are you sure you want to leave?')});

于 2017-03-16T13:50:19.860 回答
0

return不是函数,不会提示。尝试return prompt();

<html> 
<script src="jquery-3.1.1.min.js"></script>
 <body> 
<a href="https://www.science.gov" id="navigate">science</a>
 </body> 
<script> 
$(window).on('beforeunload', function(){ return prompt('Are you sure you want to leave?');});
 </script> </html>
于 2017-03-16T13:54:54.793 回答