[英]How To Call A Javascript Function From Inside Jquery When An ID is Clicked
Let's say I have a javascript function called capturedata();
假设我有一个名为
capturedata();
的JavaScript函数capturedata();
I need to call this function when this button is clicked 单击此按钮时,我需要调用此函数
<button id='yes'></button>
For some reasons, I do not want to use any event handler on the html like onclick='capturedata()'
Now I have use Jquery this way 由于某些原因,我不想在html上使用任何事件处理程序,例如
onclick='capturedata()'
现在我已经以这种方式使用了Jquery
$(document).ready(function(){
$("#yes").click();
capturedata();
});
But this is not working. 但这是行不通的。 I need a better way to handle this, and I do not want to use any event handler like onclick on the html.
我需要一种更好的方法来处理此问题,并且我不想使用任何事件处理程序,例如html上的onclick。 I want the action to happen automatically once the button is clicked.
我希望该操作一旦单击按钮即可自动发生。
Almost but not quite, you'll need to register capturedata
as the callback for the click event. 几乎但不是完全,您需要将
capturedata
注册为click事件的回调。
$(document).ready(function(){
$("#yes").click(capturedata);
});
Your click method requires a callback function as its first argument 您的click方法需要一个回调函数作为其第一个参数
eg .click(() => {})
例如
.click(() => {})
Note how calling .click()
will invoke a click event and not listen for one. 请注意,调用
.click()
将如何调用click事件而不监听一个事件。
Suggestion : use addEventListener('click', {yourCallback})
see DOCS 建议:使用
addEventListener('click', {yourCallback})
参见DOCS
$(document).ready(function(){
$("#yes").click(function(){
capturedata();
// some code
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.