调用 Jquery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15918610/
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
Call Jquery function
提问by J?cob
I have a Jquery function like the following
我有一个像下面这样的 Jquery 函数
function myFunction(){
$.messager.show({
title:'My Title',
msg:'The message content',
showType:'fade',
style:{
right:'',
bottom:''
}
});
}
If certain condition is true, I would like to invoke myFunction
and a popup message will display. How can I call myFunction? so that it will be something like onClick().
如果某些条件为真,我想调用myFunction
并显示一条弹出消息。如何调用 myFunction?这样它就会像 onClick() 一样。
回答by Adil
To call the function on click of some html element (control).
单击某些 html 元素(控件)时调用该函数。
$('#controlID').click(myFunction);
You will need to ensure you bind the event when your html element is ready on which you binding the event. You can put the code in document.ready
您需要确保在 html 元素准备好绑定事件时绑定事件。您可以将代码放在 document.ready 中
$(document).ready(function(){
$('#controlID').click(myFunction);
});
You can use anonymous function to bind the event to the html element.
您可以使用匿名函数将事件绑定到 html 元素。
$(document).ready(function(){
$('#controlID').click(function(){
$.messager.show({
title:'My Title',
msg:'The message content',
showType:'fade',
style:{
right:'',
bottom:''
}
});
});
});
If you want to bind click with many elements you can use class selector
如果您想将点击与许多元素绑定,您可以使用类选择器
$('.someclass').click(myFunction);
Editbased on comments by OP, If you want to call function under some condition
根据 OP 的评论进行编辑,如果您想在某些条件下调用函数
You can use if for conditional execution, for example,
您可以使用 if 进行条件执行,例如,
if(a == 3)
myFunction();
回答by bipen
calling a function is simple ..
调用函数很简单..
myFunction();
so your code will be something like..
所以你的代码将类似于..
$(function(){
$('#elementID').click(function(){
myFuntion(); //this will call your function
});
});
$(function(){
$('#elementID').click( myFuntion );
});
or with some condition
或者有一些条件
if(something){
myFunction(); //this will call your function
}
回答by ravisolanki07
Just add click event by jquery in $(document).ready() like :
只需在 $(document).ready() 中通过 jquery 添加点击事件,如:
$(document).ready(function(){
$('#YourControlID').click(function(){
if(Check your condtion)
{
$.messager.show({
title:'My Title',
msg:'The message content',
showType:'fade',
style:{
right:'',
bottom:''
}
});
}
});
});
回答by keny
Try this code:
试试这个代码:
$(document).ready(function(){
$('#YourControlID').click(function(){
if() { //your condition
$.messager.show({
title:'My Title',
msg:'The message content',
showType:'fade',
style:{
right:'',
bottom:''
}
});
}
});
});