javascript - js问题 这样点击怎么无效
伊谢尔伦
伊谢尔伦 2017-04-10 14:43:59
[JavaScript讨论组]

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>


<p class="">
    <a href="#" onclick="app.requireLogin('aaa','bbb');">点击</a>
</p>


</body>
</html>
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(2)
黄舟
var app={} 放到 $(function(){}) 外面

除非有对象引用,外部作用域无法访问到闭包内的变量。ready() 里面是个闭包,onclick 查找的是全局作用域,当然找不到。打开控制台看报错信息 Uncaught ReferenceError: show is not defined,也说明 onclick 访问的作用域中没有定义 show 这个标识符。你把 show 拿到外面来,就是在全局作用域中声明的,这个时候 onclick 就能找到它了。

阿神

顶上边的答案

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号