方法一:
<script type="text/javascript"> $.fn.setApDiv=function () { //apDiv浮动层显示位置居中控制 var wheight=$(window).height(); var wwidth=$(window).width(); var apHeight=wheight-$(this).height(); var apWidth=wwidth-$(this).width(); $(this).css({"position":"absolute"}) $(this).css("top",apHeight/2); $(this).css("left",apWidth/2); $(this).css({"background":"green"}); };
$(function(){ $("#apDiv").setApDiv(); }) </script> <body> <div id="apDiv"></div> </body> |
方法二:
<script type="text/javascript"> //jQuery 应用扩展 jQuery.extend({ // 设置 apDiv setApDiv:function (divString) { //apDiv浮动层显示位置居中控制 var wheight=$(window).height(); var wwidth=$(window).width(); var apHeight=wheight-$(divString).height(); var apWidth=wwidth-$(divString).width(); $(divString).css({"position":"absolute"}) $(divString).css("top",apHeight/2); $(divString).css("left",apWidth/2); $(divString).css({"background":"green"}); } });
$(function(){ $.setApDiv("#apDiv"); })</script> <body> <div id="apDiv"></div> </body> |
总结: 一种如$.extend({'aa':function(){}}),这种调用时就是这样$.aa(),另一种如$.fn.extend({'aa':function(){}}),这种调用时就得这样,$(this).aa()
方法三:
<script type="text/javascript"> $.xyz = function(num1,num2){ var num11=parseInt(num1); var num22=parseInt(num2); alert(num1+"+"+num2+"="+(num11+num22)); }
$(function(){ $.xyz("11","44"); }) </script> |