jquery - JavaScript 我弄了俩盒子,用了一个 事件 function ,怎么第二个找不到 function ?
大家讲道理
大家讲道理 2017-04-11 11:07:27
[JavaScript讨论组]







   

//★★就是这个地方,提示:"Uncaught ReferenceError: show is not defined"

就是★★的地方,提示:"Uncaught ReferenceError: show is not defined"

我要是把
$(document).ready(function(){    /***加载事件的开始,这行就不别改了!**/ 
删了,第一个 

又不管事了...

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(3)
ringa_lee

楼主应该这样写吧

function show(){
    console.log( '********' );
}
$(document).ready(function(){   
    
    document.getElementById("one").onmouseenter=show;

})    

如果你定义在reday里面的function实际上这是一个私有作用域,show只能在function() {}里面使用;而two绑定的函数必须是在全局中可以调用到。

迷茫

show的作用域不对:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){    /***加载事件的开始,这行就不别改了!**/
    
            document.getElementById("one").onmouseenter=show;
            
            window.show = function(){console.log( '********' );};

        })
    </script>
    <style>p{width:100px;height:100px;background-color:red;margin:0 auto;}</style>
</head>

<body>

   <p id="one"></p>

   <p onmouseenter="show()" id="two" ></p>
</body>
</html>
高洛峰

楼主请参考楼上的两种处理方法

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

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