用click()调用jQuery中的函数

时间:2022-09-21 00:01:03

In the code below, why does the open function work but the close function does not?

在下面的代码中,为什么open函数有效但close函数没有?

$("#closeLink").click("closeIt");

How do you just call a function in click() instead of defining it in the click() method?

你如何在click()中调用一个函数而不是在click()方法中定义它?

<script type="text/javascript">
    $(document).ready(function() {
        $("#openLink").click(function() {
            $("#message").slideDown("fast");
        });
       $("#closeLink").click("closeIt");
    });

    function closeIt() {
        $("#message").slideUp("slow");
    }
</script>

My HTML:

我的HTML:

Click these links to <span id="openLink">open</span> 
and <span id="closeLink">close</span> this message.</div>

<div id="message" style="display: none">This is a test message.</div>

1 个解决方案

#1


118  

$("#closeLink").click(closeIt);

Let's say you want to call your function passing some args to it i.e., closeIt(1, false). Then, you should build an anonymous function and call closeIt from it.

假设您想调用函数将一些args传递给它,即closeIt(1,false)。然后,您应该构建一个匿名函数并从中调用closeIt。

$("#closeLink").click(function() {
    closeIt(1, false);
});

#1


118  

$("#closeLink").click(closeIt);

Let's say you want to call your function passing some args to it i.e., closeIt(1, false). Then, you should build an anonymous function and call closeIt from it.

假设您想调用函数将一些args传递给它,即closeIt(1,false)。然后,您应该构建一个匿名函数并从中调用closeIt。

$("#closeLink").click(function() {
    closeIt(1, false);
});