我想了解如何在 jquery 中编写自定义函数。
我有这个代码:
<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
到 jQuery 原型,它可以让你调用$("div").alertMe();
要按原样调用它,只需声明一个常规函数:
function alertMe () {
alert("hello world!");
}
Also, $("createaccountbtn")
不是一个有效的选择器,因为它会尝试选择 tagName 为createaccountbtn
- 确保在其前面加上#
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)