(function( $ ) { $.fn.showhtml = function() { alert(this.html()); }; })( jQuery );
$.fn.extend({ showhtml : function() { alert(this.html()); }, showtext : function() { alert(this.text()); } });
jQuery.extend({ add:function(a,b){ returna+b; }, multi:function(a,b){ returna*b; } });
$.expr[':'].withjQuery = function(obj){ // 自定义选择器的代码 return 匹配HTML元素的条件,; };
$.expr[':'].withjQuery = function(obj){ var $this = $(obj); return ($this.html().indexOf('jQuery') >-1); };
使用JQuery插件的方法:
在jQuery程序中使用jQuery插件的方法很简单,首先需要引用jQuery脚本和插件脚本
<1>使用封装JQuery对象方法的使用:
jQuery对象.封装的jQuery对象方法
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script src="jquery.showhtml.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $("#p1").click(function(){ $("#p1").showhtml(); }); }); </script> </head> <body> <p id="p1">单击我</button> </body> </html>
jQuery.全局函数名();
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script src="jquery.myFunc.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $("#p1").click(function(){ $("#p1").html("1+2="+jQuery.add(1,2)); }); }); </script> </head> <body> <p id="p1">单击我</button> </body> </html>
<html> <head> <script type="text/javascript" src="jquery.js"></script> <script src="jquery.customselector.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $('p:withjQuery').css('background-color', 'yellow'); }); </script> </head> <body> <p >jQuery属于Java家族,它是一种快捷、小巧、功能丰富的JavaScript库。jQuery提供很多支持各种浏览器平台的API,使用这些API可以使Web前端开发变得更加轻松。</button><br> <p>HTML5是最新的HTML标准。之前的版本HTML4.01于1999年发布。10多年过去了,互联网已经发生了翻天覆地地变化。</button> </body> </html>