js问题 这样点击怎么无效
报错:Uncaught ReferenceError: app is not defined
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script>
$(function(){
$("#login_frame").bind({
click:function(){
$(".close").parent().parent().remove();
}
});
var app = {
requireLogin : function(a, b) {
console.log(a);
console.log(b);
}
};
});
</script>
</head>
<body>
<div class="">
<a href="#" onclick="app.requireLogin('aaa','bbb');">点击</a>
</div>
</body>
</html>
除非有对象引用,外部作用域无法访问到闭包内的变量。ready() 里面是个闭包,onclick 查找的是全局作用域,当然找不到。打开控制台看报错信息 Uncaught ReferenceError: show is not defined,也说明 onclick 访问的作用域中没有定义 show 这个标识符。你把 show 拿到外面来,就是在全局作用域中声明的,这个时候 onclick 就能找到它了。