jQuery 模拟点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15316272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:49:07  来源:igfitidea点击:

jQuery simulate click

javascriptjquery

提问by Bwyss

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

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

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

error: getType is not defined

错误:未定义 getType

What am I doing wrong?

我究竟做错了什么?

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

只是为了澄清,在这种情况下我不能使用匿名函数。此外,我是否使用$(document).ready或对我来说无关紧要$(window).bind("load", function(),但是使用这些我仍然会收到“未定义 getType”错误。

回答by Blender

You either have to make your function anonymous:

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

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

Or pass the function itself:

或者传递函数本身:

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

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

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

如果您只想触发点击,请调用.click()

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

Also, your idparameter 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:

此外,您的id参数不会是元素的id. 这将是点击事件对象。要获取元素的id,您可以使用 引用被点击的元素this

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

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

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

回答by Abu Roma?ssae

You are using the inline notation so, you should use an anonymous function (no name function)

您正在使用内联符号,因此您应该使用匿名函数(无名称函数)

your code should be:

你的代码应该是:

$('#button').click(function() {
      // do your stuff here
    }
);


Besidethat, as the titles says, you need to simulate a clickevent, right ? if so you better use something like:

除此之外,正如标题所说,您需要模拟一个click事件,对吗?如果是这样,你最好使用类似的东西:

$('#button').on('click', function() {
  alert($(this).text());
});
// somewhere when you want to simulate the click you call the trigger function
$('#button').trigger('click');

see documentation here

请参阅此处的文档

回答by Gabriel Santos

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

Should be:

应该:

$('#button').click(function() {
        [...] code here
    }
);

function() { }is a callback with what code have to do when I click some element.

function() { }是当我单击某个元素时代码必须执行的回调。

If you have the getTypefunction, you can pass it as a callback:

如果你有这个getType函数,你可以将它作为回调传递:

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

If you want to trigger a funcion, when page load, you can do this:

如果你想触发一个函数,当页面加载时,你可以这样做:

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

Or

或者

function getType() {
    [...] code here
}

getType();

回答by Wex

Use .trigger( event [, extraParameters ] )on the element.

.trigger( event [, extraParameters ] )在元素上使用。

extraParameters
Type: Array or PlainObject
Additional parameters to pass along to the event handler.

extraParameters
类型:Array 或PlainObject
要传递给事件处理程序的附加参数。

The added benefit is that you can pass data to the event handler, whereas if you use .click(), you cannot assign data to the object.

额外的好处是您可以将数据传递给事件处理程序,而如果使用.click(),则无法将数据分配给对象。

$("#target").trigger('click');

If you're looking to use the extraParameters:

如果您想使用extraParameters

$( "#foo" ).on( "custom", function( event, param1, param2 ) {
  alert( param1 + "\n" + param2 );
});
$( "#foo").trigger( "custom", [ "Custom", "Event" ] );

回答by Starx

The .click()method requires a callback function. So you can do something like this instead:

.click()方法需要一个回调函数。所以你可以做这样的事情:

//Define your function somewhere else
function getType(id) {
    //...some code
}

$('#button').click(function() {
    getType($(this).attr('id')); //Execute it when its clicked.
});

回答by Niels

Try this, the id can not be passed the way you do:

试试这个,id 不能按照你的方式传递:

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