自定义jQuery函数 [英] Custom jQuery Functions
问题描述
我有这样的代码:
< HEAD>
。
。
< script src =js / jquery.js>< / script>
< script> ()函数(e){
e.preventDefault();
alertMe() ;
});
});
$ b $(函数($){
$ .fn.alertMe = function(){
alert(hello world!);
};
})(jQuery);
< / script>
< / head>
< body>
< h1>创建帐户< / h1>
< div id =wrapper>
< form name =createaccountformaction =php / createaccount.phpmethod =post>
< p>名字< input name =fNametype =textsize =25/>< label id =fNameStatus>< / label>< / p>
< p>姓氏<输入名称=lNametype =textsize =25/>< label id =lNameStatus>< / label>< ; / p为H.
< p>电子邮件< input name =emailtype =textsize =25/>< label id =emailStatus>< / label> < / p为H.
< p>确认电子邮件< input name =emailtype =textsize =25/>< label id =emailStatus>< / label> ;< / p为H.
< p>密码< input name =passtype =textsize =25/>< label id =passwordStatus>< / label>< / p为H.
< p>确认密码< input name =cpasstype =textsize =25/>< id name =cpasswordStatus>< / label>< ; / p为H.
< p>< input id =createaccountbtntype =buttonvalue =Create Account/>< / p>
< / form>
< / div>
< / body>
我试图在用户点击Create Account按钮时执行一个自定义函数,但是我不能让它工作。任何想法是什么?
此代码:
(function($){
$ .fn.alertMe = function(){
alert(hello world!);
};
})(jQuery);
为jQuery添加了一个名为 alertMe
的函数原型,它可以让你调用 $(div)。alertMe();
你只需声明一个常规函数: 另外, I am trying to understand how to write custom functions in jquery. I have this code: I am trying to execute a custom function when the user clicks the "Create Account" button, but I cannot get it to work. Any ideas what's up? This code: adds a function called To call it as you are, just declare a regular function: Also, 这篇关于自定义jQuery函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ function alertMe(){
alert(hello world!) ;
$ / code> $(createaccountbtn) code>不是一个有效的选择器,因为它会尝试选择一个tagName为
createaccountbtn
的任何东西 - 确保在它前面加上一个 #
。<head>
.
.
<script src="js/jquery.js"></script>
<script>
$(document).ready(function(){
$("createaccountbtn").click(function(e){
e.preventDefault();
alertMe();
});
});
(function( $ ) {
$.fn.alertMe = function() {
alert("hello world!");
};
})( jQuery );
</script>
</head>
<body>
<h1>Create Account</h1>
<div id = "wrapper">
<form name = "createaccountform" action = "php/createaccount.php" method = "post">
<p>First Name<input name = "fName" type="text" size="25"/><label id ="fNameStatus"></label></p>
<p>Last Name<input name = "lName" type="text" size="25"/><label id ="lNameStatus"></label></p>
<p>E-mail<input name = "email" type="text" size="25"/><label id ="emailStatus"></label></p>
<p>Confirm E-mail<input name = "email" type="text" size="25"/><label id ="emailStatus"></label></p>
<p>Password<input name = "pass" type="text" size="25"/><label id ="passwordStatus"></label></p>
<p>Confirm Password<input name = "cpass" type="text" size="25"/><id name ="cpasswordStatus"></label></p>
<p><input id = "createaccountbtn" type ="button" value="Create Account"/></p>
</form>
</div>
</body>
(function( $ ) {
$.fn.alertMe = function() {
alert("hello world!");
};
})( jQuery );
alertMe
to the jQuery prototype, which will let you call $("div").alertMe();
function alertMe () {
alert("hello world!");
}
$("createaccountbtn")
is not a valid selector, as it will try to select anything with a tagName of createaccountbtn
- make sure to prepend it with a #
.