Javascript 如何在jquery事件处理程序中使用参数调用javascript函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7510654/
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
How to call javascript function with parameter in jquery event handler?
提问by mhd
I am stuck. Searched and tried for hours.
我被困住了。搜索并尝试了几个小时。
EDIT: I still can't make it work. Okay, I'll just put the source code to make it clear what I want to accomplish.
编辑:我仍然无法让它工作。好吧,我只是把源代码,以明确我想完成什么。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript">
var date_fmt="yyyy-mm-dd";
var time_fmt="HH:MM";
var date_field="#id_start_0, #id_end_0"; //id refering to html input type='text'
var time_field="#id_start_1, #id_end_1"; //id refereing to html input type='text'
function clearFmt(fmt_type)
{
if($(this).val() == fmt_type) {
$(this).val("");
}
}
function putFmt(fmt_type)
{
if($(this).val() == "") {
$(this).val(fmt_type);
}
}
$(document).ready(function() {
$(date_field).attr("title",date_fmt);
$(time_field).attr("title",time_fmt);
$(date_field).click(function() {
clearFmt(date_fmt);
});
$(date_field).blur(function(){
putFmt(date_fmt);
});
$(time_field).click(function(){
clearFmt(time_fmt);
});
$(time_field).blur(function(){
putFmt(time_fmt);
});
});
</script>
Help ?
帮助 ?
回答by scessor
Use the jquery bindmethod:
使用 jquery绑定方法:
function myfunc(param) {
alert(param.data.key);
}
$(document).ready( function() {
$("#foo").bind('click', { key: 'value' }, myfunc);
});
Also see my jsfiddle.
另请参阅我的jsfiddle。
=== UPDATE ===
=== 更新 ===
since jquery 1.4.3 you also can use:
从 jquery 1.4.3 开始,您还可以使用:
function myfunc(param) {
alert(param.data.key);
}
$(document).ready( function() {
$("#foo").click({ key: 'value' }, myfunc);
});
Also see my second jsfiddle.
另请参阅我的第二个jsfiddle。
=== UPDATE 2 ===
=== 更新 2 ===
Each function has his own this
. After calling clearFmt
in this function this
is no longer the clicked element. I have two similar solutions:
每个功能都有自己的this
. clearFmt
在这个函数中调用后this
不再是被点击的元素。我有两个类似的解决方案:
In your functions add a parameter called e.g. element
and replace $(this)
with element
.
在您的函数中添加一个名为 eg 的参数element
并替换$(this)
为element
.
function clearFmt(element, fmt_type) {
if (element.val() == fmt_type) {
element.val("");
}
}
Calling the function you have to add the parameter $(this)
.
调用函数你必须添加参数$(this)
。
$(date_field).click(function() {
clearFmt($(this), date_fmt);
});
Also see my third jsfiddle.
另请参阅我的第三个 jsfiddle。
-=-
-=-
An alternative:
替代:
function clearFmt(o) {
if ($(o.currentTarget).val() == o.data.fmt_type) {
$(o.currentTarget).val("");
}
}
$(date_field).click({fmt_type: date_fmt}, clearFmt);
Also see my fourth jsfiddle.
另请参阅我的第四个 jsfiddle。
回答by Darin Dimitrov
回答by Abdur Rahman
anyFunctionName = (function()
{
function yourFunctionName1()
{
//your code here
}
function yourFunctionName2(parameter1, param2)
{
//your code here
}
return
{
yourFunctionName1:yourFunctionName1,
yourFunctionName2:yourFunctionName2
}
})();
$document.ready(function()
{
anyFunctionName.yourFunctionName1();
anyFunctionName.yourFunctionName2();
});
Note: if you don't use 'anyFuctionName' to declare any function then no need return method. just write your function simply like, yourFunctionName1().
注意:如果您不使用 'anyFuctionName' 来声明任何函数,则不需要返回方法。只需像 yourFunctionName1() 一样编写您的函数。
in $document.ready section parameter is not issue. you just put your function name here. if you use 'anyFuctionName' function then you have to follow above format.
在 $document.ready 部分参数不是问题。你只要把你的函数名放在这里。如果您使用“anyFuctionName”函数,则必须遵循上述格式。
回答by Hammad Khan
People are making this complicated, simply call directly as you would in Javascript
人们正在使这变得复杂,只需像在 Javascript 中一样直接调用
myfunc("value");
myfunc("值");
回答by Ariel
function myfunc(e) {
alert(e.data.bar); //this is set to "value"
}
$("#foo").click({bar: "value"}, myfunc);