JQuery自定义插件

说明:使用JQuery中的JQuery.extend({//这里是json格式的数据});可以定义一个全局函数

使用JQuery中的JQuery.fn.extend({//这里是json格式的数据});可以定义一个局部函数

1.定义全局函数(来判断两个输入的数字的最大值和最小值)

test.html

<!DOCTYPE html>
<html>
  <head>
    <title>test.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<!-- 引入jQuery文件 -->
    <script src="../../js/jquery-1.4.2.js"></script>
	<!-- 引入jQuery的插件文件 -->
	<script src="test.js"></script>
  </head>
  <script type="text/javascript">
  	$(function(){
		$("input").click(function(){
			var a=prompt("请输入一个数字");
			var b=prompt("请输入一个数字");
			var min=$.minValue(a,b);
			var max=$.maxValue(a,b);
			alert("最大值是"+max+"最小值是"+min);
			
		});
	})
	
  </script>
  <body>
    <input type="button" value="jQuery插件扩展测试">
  </body>
</html>
test.js

jQuery.extend({
	minValue:function(a,b){
		return a<b?a:b;
	},
	maxValue :function(a,b){
		return a>b?a:b;
	}
});

结果演示:



2.自定义局部函数(提示当前元素中value的值)
test.html

<!DOCTYPE html>
<html>
  <head>
    <title>test.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script src="../../js/jquery-1.4.2.js"></script>
	<script src="test.js"></script>
  </head>
  <script type="text/javascript">
  	$(function(){
		$("input").click(function(){
			//this指代页面元素input标签
			$(this).test();
		});
	})
  </script>
  <body>
    <input type="button" value="jQuery插件扩展测试">
  </body>
</html>
test.js

测试extend()方法
jQuery.fn.extend({
	test : function(){
		//谁调用test()方法,打印谁的value属性值
		//this指代DOM对象还是jQuery对象?jQuery对象
		//jQuery对象是数组对象,数组可以遍历
		$.each(this,function(){
			alert($(this).val());
		});
	}
});
结果演示:





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery中,我们可以使用两种方式来添加自定义函数。第一种方式是使用$.fn.extend方法或jQuery.fn来扩展jQuery对象的原型,例如$.fn.extend({'aa':function(){}}) 或 jQuery.fn.aa=function(){}。使用这种方式添加的自定义函数需要通过选择器来调用,例如$("#**").aa()。 另一种方式是使用$.extend方法来直接向jQuery中添加自定义函数,例如$.extend({'aa':function(){}})。使用这种方式添加的自定义函数可以直接通过函数名来调用,例如$.aa()。 通过以上两种方式,我们可以向jQuery中添加自定义的新函数来满足我们的特定需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [jquery 自定义函数方法](https://blog.csdn.net/a241903820/article/details/80034009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [【jQuery】如何添加自定义函数及封装自定义插件_09](https://blog.csdn.net/sunyctf/article/details/125665104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值