使用click()在jQuery中调用函数 [英] Calling a function in jQuery with click()

查看:519
本文介绍了使用click()在jQuery中调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,为什么打开函数可以工作,但关闭函数不会?

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

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

你如何调用中的一个函数点击(),而不是在< b $ b

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

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

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

我的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>


推荐答案

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

假设您要调用您的函数传递一些参数,即 closeIt(1,false)。然后,你应该建立一个匿名函数并从中调用 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.

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

这篇关于使用click()在jQuery中调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆