简体   繁体   English

$(文件).ready似乎开火但没有注册活动

[英]$(document).ready Seems to Fire but not Register Events

On load the two events are fired, but not registered with the specified event handlers. 在加载时,会触发两个事件,但不会在指定的事件处理程序中注册。 The code that is not behaving is: 不行为的代码是:

            $("#calendar_menu_item").bind('click', loadCalendarContent() );
            $("#patient_menu_item").bind('click', loadPatientContent() );   

This is the whole script: 这是整个脚本:

    /* Add stuff only once the DOM is loaded. */
    $(document).ready
        (
        function(){ 
            //Initialize the main menu  
            var menuItems = [$("#calendar_menu_item"), $("#patient_menu_item")];

            for (i = 0; i < menuItems.length; i++) {
                var menuItem = menuItems[i];

                menuItem.bind('mouseover', function(){
                    $(this).css("background-color", "#749ccf");
                });

                menuItem.bind('mouseout', function(){
                    $(this).css("background-color", "#506077");                     
                });
            }       

            $("#calendar_menu_item").bind('click', loadCalendarContent() );
            $("#patient_menu_item").bind('click', loadPatientContent() );               
        }
        );  

    function loadCalendarContent(){
        $("#content_area").load('calendar.html');           
    }

    function loadPatientContent(){
        $("#content_area").load('patient.html');            
    }       

    function doAction(){
        alert( "in doScript()");
    }

You need to change: 你需要改变:

        $("#calendar_menu_item").bind('click', loadCalendarContent() );
        $("#patient_menu_item").bind('click', loadPatientContent() ); 

should be: 应该:

        $("#calendar_menu_item").bind('click', loadCalendarContent );
        $("#patient_menu_item").bind('click', loadPatientContent ); 

The reason for this being that loadCalendarContent (and loadPatientContent too) doesn't return anything, so loadPatientContent() is undefined. 原因是loadCalendarContent(和loadPatientContent)也没有返回任何内容,因此loadPatientContent()是未定义的。 In other word, you try to run undefined everytime #calendar_menu_item is clicked. 换句话说,每次单击#calendar_menu_item ,都会尝试运行undefined If you wish to execute the functions at domready too (document.ready), you can do that with the following code: 如果您希望在domready上执行这些功能(document.ready),您可以使用以下代码执行此操作:

        loadCalendarContent();
        loadPatientContent();
        $("#calendar_menu_item").bind('click', loadCalendarContent );
        $("#patient_menu_item").bind('click', loadPatientContent ); 

You're not quite understanding functions as first-class objects. 你不太了解函数作为一等对象。 When you see this: 当你看到这个:

foo()

It is a function called foo that is being called. 这是一个被称为foo的函数。 When you see this: 当你看到这个:

foo

It is a variable. 这是一个变量。 Variables can contain functions. 变量可以包含函数。

Your code is like this: 你的代码是这样的:

$(element).bind('click', foo() );

When you see the () after foo , it means that it's calling the function and replacing the reference to it with its return value. 当你在foo之后看到() ,它意味着它正在调用函数并用它的返回值替换对它的引用。 This isn't what you want. 这不是你想要的。 What you want is to simply pass the function as a variable. 你想要的是简单地将函数作为变量传递。 That would look like this: 这看起来像这样:

$(element).bind('click', foo);

That way, the callback is registered with a reference to the function, and that function can be called when necessary, not straight away. 这样,回调是通过对函数的引用进行注册的,并且可以在必要时调用该函数,而不是直接调用。

Specifically, your code should look like the following: 具体来说,您的代码应如下所示:

$("#calendar_menu_item").bind('click', loadCalendarContent);
$("#patient_menu_item").bind('click', loadPatientContent);

loadCalendarContent is the reference to the function, but loadCalendarContent() executes the function, so change this: loadCalendarContent是对函数的引用,但loadCalendarContent()执行函数,所以改变这个:

$("#calendar_menu_item").bind('click', loadCalendarContent() );

to

$("#calendar_menu_item").bind('click', loadCalendarContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM