jQuery模拟点击 [英] jQuery simulate click

查看:66
本文介绍了jQuery模拟点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在加载页面时触发一个函数。 有很多方法可以做到这一点。
但是,当我添加 $('#button')。点击我的函数前面的,然后无法识别 getType 函数。例如:

I want to trigger a function when the page is loaded. There are many ways to do this. However, when I add $('#button').click in front of my function, then the getType function is not recognized. For example:

$('#button').click(function getType(id) {
    //...some code
});




错误:未定义getType

error: getType is not defined

我做错了什么?

为了澄清,在这种情况下我不能使用匿名函数。另外,对我来说无论是否使用 $(document).ready $(window).bind(load,function( ),但使用这些我仍然得到getType未定义错误。

Just to clarify, in this case I cannot use an anonymous function. Also, it does not matter to me whether I use $(document).ready or $(window).bind("load", function(), but using these I still get the "getType is not defined" error.

推荐答案

你要么必须使你的函数匿名:

You either have to make your function anonymous:

$('#button').click(function() {
    //...some code
});

或传递函数本身:

function getType() {
    //...some code
}

$('#button').click(getType);

如果你只想触发点击一下,拨打 .click()

If you just want to trigger a click, call .click():

$('#button').click();

此外,您的 id 参数不是元素的 id 。它将是click事件对象。要获取元素的 id ,你可以使用这个来引用被点击的元素:

Also, your id parameter won't be the element's id. It'll be the click event object. To get the element's id, you can refer to the clicked element using this:

$('#button').click(function() {
    var id = this.id;
});

我建议您阅读一些JavaScript和jQuery教程(按此顺序)。

I suggest you read a few JavaScript and jQuery tutorials (in that order).

这篇关于jQuery模拟点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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