<divid="main"> <p>Take a look at the source to see how messages can be customized with metadata.</p> <!-- Custom rules and messages via data- attributes --> <formclass="cmxform"id="commentForm"method="post"action=""> <fieldset> <legend>Please enter your email address</legend> <p> <labelfor="cemail">E-Mail *</label> <inputid="cemail"name="email"data-rule-required="true"data-rule-email="true"data-msg-required="Please enter your email address"data-msg-email="Please enter a valid email address"> </p> <p> <inputclass="submit"type="submit"value="Submit"> </p> </fieldset> </form> </div> <script> $(document).ready(function() { $("#commentForm").validate(); }); </script>
<divid="main"> <formclass="cmxform"id="texttests"method="get"action="foo.html"> <h2id="summary"></h2> <fieldset> <legend>Example with custom methods and heavily customized error display</legend> <table> <tr> <td> <labelfor="number">textarea</label> </td> <td> <inputid="number"name="number"title="Please enter a number with at least 3 and max 15 characters!"> </td> <td></td> </tr> <tr> <td> <labelfor="secret">Secret</label> </td> <td> <inputname="secret"id="secret"> </td> <td></td> </tr> <tr> <td> <labelfor="math">7 + 4 =</label> </td> <td> <inputid="math"name="math"title="Please enter the correct result!"> </td> <td></td> </tr> </table> <inputclass="submit"type="submit"value="Submit"> </fieldset> </form> <h3id="warning">Your form contains tons of errors! Please try again.</h3> </div> <script> // this one requires the text "buga", we define a default message, too $.validator.addMethod("buga", function(value) { return value == "buga"; }, 'Please enter "buga"!'); // this one requires the value to be the same as the first parameter $.validator.methods.equal = function(value, element, param) { return value == param; }; $().ready(function() { var validator = $("#texttests").bind("invalid-form.validate", function() { $("#summary").html("Your form contains " + validator.numberOfInvalids() + " errors, see details below."); }).validate({ debug: true, errorElement: "em", errorContainer: $("#warning, #summary"), errorPlacement: function(error, element) { error.appendTo(element.parent("td").next("td")); }, success: function(label) { label.text("ok!").addClass("success"); }, rules: { number: { required: true, minlength: 3, maxlength: 15, number: true }, secret: "buga", math: { equal: 11 } } }); }); </script>
jQuery.globalFunction = function(){ alert("This is my plugins "); }
第二种定义方式
1 2 3 4 5 6 7 8
jQuery.extend({ functionOne : function(){ alert("This is one function"); }, functionTwo : function(param){ alert("This is two function , param is "+param); } });
第三种定义方式
1 2 3 4 5 6 7
jQuery.sum = function(array){ var total = 0; jQuery.each(array,function(index,value){ total += value; }); return total; }
jQuery插件案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
jQuery.fn.shadow = function(){ returnthis.each(function(){ var $originalElement = jQuery(this); for(var i=0; i < 5 ; i ++){ $originalElement.clone().css({ position:'absolute', left:$originalElement.offset().left + i, top:$originalElement.offset().top + i, margin:0, zIndex:-1, opacity:0.1 }).appendTo('body'); } }); }