【发布时间】:2018-06-15 22:07:50
【问题描述】:
当用户单击“关闭”链接时,该窗口应该关闭。相反,它在打开时立即关闭。这是怎么回事?
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
p {
text-align: center;
}
</style>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>
<img src = "assets/images/trophy.png"></img>
<p>YAY! U R TEH WINZ!</p>
<p><a href = "#" class = "exit">Close.</a></p>
<script>
$(".exit").onClick(this.window.close());
</script>
</body>
</html>
<!-- UGH, what is going on? Either clicking takes you nowhere, or it happens
right on load! -->
【问题讨论】:
-
这是执行顺序,函数参数在调用函数之前被评估。在这种特殊情况下,评估参数会关闭窗口。几乎任何其他参数都会出现类型错误,
onClick不是 jQuery 方法。正确的语法是$(".exit").click((e) => {this.window.close();});。
标签: javascript jquery html window