调用Jquery函数 [英] Call Jquery function
问题描述
我有一个类似于以下内容的Jquery函数
I have a Jquery function like the following
function myFunction(){
$.messager.show({
title:'My Title',
msg:'The message content',
showType:'fade',
style:{
right:'',
bottom:''
}
});
}
如果某些条件为真,我想调用myFunction
,将显示一个弹出消息.如何调用myFunction?这样就可以像onClick()一样.
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().
推荐答案
在单击某些html元素(控件)时调用该函数.
To call the function on click of some html element (control).
$('#controlID').click(myFunction);
您需要确保在绑定事件的html元素准备就绪时绑定事件.您可以将代码放入document.ready
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
$(document).ready(function(){
$('#controlID').click(myFunction);
});
您可以使用匿名函数将事件绑定到html元素.
You can use anonymous function to bind the event to the html element.
$(document).ready(function(){
$('#controlID').click(function(){
$.messager.show({
title:'My Title',
msg:'The message content',
showType:'fade',
style:{
right:'',
bottom:''
}
});
});
});
如果您想将click与许多元素绑定在一起,则可以使用类选择器
If you want to bind click with many elements you can use class selector
$('.someclass').click(myFunction);
编辑,基于OP的注释,如果您希望在某些情况下调用函数
Edit based on comments by OP, If you want to call function under some condition
您可以将if用于条件执行,例如
You can use if for conditional execution, for example,
if(a == 3)
myFunction();
这篇关于调用Jquery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!