douzhang1299 2014-05-06 19:01
浏览 54

可见性JQuery函数(ready(),change()...)

I have 3 Files:

  1. Php file where i include the other two files like this:

    <head>
        <!-- 1. Javascript File-->
        <script src="js/functions.js"></script>
    </head>
    <html>
           <?php include 'includes/question1.html' ?>
    </html>
    
  2. The functions.js file looks like this:

    $(document).ready(function(){
    
        function showError(){
    
              alert('ERROR');
        }
    });
    
    $('input').change(function(){
    
         // SOME STUFF HERE 
    
    });
    

But Now i want to use the "showError" Function or the "change" Function in the third data or in the normal Php file but it says everytime that there is no function like this.

It's only a problem of visibility how can i change this ?

  • 写回答

1条回答 默认 最新

  • doushi6932 2014-05-06 19:59
    关注

    The problem doesn't have anything to do with the files, they're fine if jQuery is found and the js is executes (which seems to work as you get an error from it).

    Your problem is the scope in which showError is available. You've declared it inside the jQuery-ready handler, which means that it is only available in there and not from the outside.

    Also, you should put DOM selections like $('input') always inside the ready handler - that's what it was made for.

    So this will work:

    $(document).ready(function(){
        function showError(){
            alert('ERROR');
        }
    
        $('input').change(function(){
            // SOME STUFF HERE
            // including calls to showError();
        });
    });
    

    Actually, you could even move the showError declaration out of the handler into the global scope.

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名