[英]Define a function globally in jQuery
I have some simple jQuery code, and it has a problem. 我有一些简单的jQuery代码,但是有问题。 The menu handler function doesn't work at all. 菜单处理程序功能根本不起作用。
var clicked = false;
$(document).ready(function(){
$TemplateMenu= $('<p class="paragraph">texxt</p>');
$('.TemplateMaker').click();
this.menuhandler();
});
});
function menuhandler(){
if(clicked == false){
$(this).after($TemplateMenu);
clicked = true;
}
else{
$TemplateMenu.remove();
clicked = false;
}
For some reason, the function works if I put it directly inside click() like this: 由于某种原因,如果将函数直接放置在click()中,则该函数将像这样运行:
$('.TemplateMaker').click(function(){
if(clicked == false){
$(this).after($TemplateMenu);
clicked = true;
}
else{
$TemplateMenu.remove();
clicked = false;
}
});
});
What is wrong with this code? 此代码有什么问题? Did I define the function wrong or do I need something special if the function contain jQuery elements? 如果函数包含jQuery元素,我是否定义了错误的函数?是否需要一些特殊的东西? Thanks for the help :-) 谢谢您的帮助 :-)
Edit: 编辑:
I edit the code to include your guys suggestions, its stile doesn't seem to work. 我编辑了代码以包含你们的建议,但它的步骤似乎无效。 code: 码:
var clicked = false;
$(document).ready(function(){
$TemplateMenu= $('<p class="paragraph">texxt</p><p class="p2">texxt</p>');
$('.TemplateMaker').click(menuHandler($(this)));
});
function menuHandler(obj){
if(clicked == false){
$(obj).after($TemplateMenu);
clicked = true;
}
else{
$TemplateMenu.remove();
clicked = false;
}}
I notic now that the jquery throw this "event.returnValue is deprecated. Please use the standard event.preventDefault() instead. ", but I don't know how its contact to my script. 我现在注意到jQuery抛出了“不赞成使用event.returnValue。请改用标准event.preventDefault()。”,但是我不知道它如何与我的脚本联系。
$('.TemplateMaker').click();
this.menuhandler();
});
Try replacing this.menuhandler(); 尝试替换this.menuhandler(); with just menuhandler(); 只需menuhandler(); as shown below: 如下所示:
$('.TemplateMaker').click();
menuhandler();
});
Edit: In response to your comment. 编辑:回应您的评论。 Try using this instead of $(this) in the menuhandler() of your original code. 尝试使用此代码代替原始代码的menuhandler()中的$(this) 。
You probably want the menuHandler function to fire when someone clicks on the button? 您可能希望当有人单击按钮时触发menuHandler函数? Then you can simply change your code as follows: 然后,您可以简单地如下更改代码:
$('.TemplateMaker').click(menuHandler);
Edit: You basically have two options: 编辑:您基本上有两个选择:
Option1: 选项1:
You don't have to pass $(this)
change your code to this (make sure you remove the parameter in the menuHandler function if you choose this approach): 您不必传递$(this)
将代码更改为此(如果选择此方法,请确保删除menuHandler函数中的参数):
$(document).ready(function(){
$TemplateMenu= $('<p class="paragraph">texxt</p><p class="p2">texxt</p>');
$('.TemplateMaker').click(menuHandler);
});
Option2: 选项2:
Or if you want to pass $(this)
you can do something like this (keep the parameter in the menuHandler function if you choose this approach): 或者,如果您想传递$(this)
,则可以执行以下操作(如果选择此方法,请保留menuHandler函数中的参数):
$(document).ready(function(){
$TemplateMenu= $('<p class="paragraph">texxt</p><p class="p2">texxt</p>');
$('.TemplateMaker').click(function() {
menuHandler($(this));
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.