jQury的工具封装例子

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
		<label>a,<input type="checkbox" name=""/></label>
		<label>b,<input type="checkbox" name=""/></label>
		<label>b,<input type="checkbox" name=""/></label>
		<label>c,<input type="checkbox" name=""/></label>
		<label>c,<input type="checkbox" name=""/></label>
		<label>c,<input type="checkbox" name=""/></label>
		<label>d,<input type="checkbox" name=""/></label>
		<label>d,<input type="checkbox" name=""/></label>
		<div class="">
			<button>全选</button>
			<button>全不选</button>
			<button>反选</button>
		</div>
	</body>
</html>
<script src="js/jquery-2.1.0.js"></script>
<script>
	//将全选全不选,反选,写入$.fn中
	$.fn.extend({
		//全选
		checkall:function(){
			$(this).prop('checked',true)
		},
		//全不选
		notall:function(){
			$(this).prop('checked',false)
		},
		//反选
		reserve:function(){
			$(this).prop('checked', !$(this).prop('checked'))
		}
		
		
	})
	//去掉字符串前面的空格的方法的封装
	$.extend({
		//去掉字符串前面的空格 传一个参数为字符串
		rmspacefront: function(str){
			//定义一个需要的正则表达式 去掉字符串前面的空格
			var regs = /^\s*/
			//去掉str前的空格
			str = str.replace(regs,'')
			return str
		},
		//去掉字符串后面的空格
		rmspaceback: function(){
			//定义一个需要的正则表达式
			var regs = /\s*$/
			//去掉str后面的空格
			str = str.replace(regs,'')
			return str
		},
		//去掉首尾的空格
		rmspaceFandB: function(){
			//定义一个需要的正则表达式
			var regs1 = /^\s*/
			var regs2 = /\s*$/
			//去掉字符串前面和后面的空格
			str = str.replace(regs1,'').replace(regs2,'')
			return str
		},
		//去掉字符串中的所有的空格
		rmspaceall:function(){
			//定义一个需要的正则表达式
			var regs = /\s*/g
			str = str.replace(regs,'')
			return str
		}
		
	})
	//给全选button按钮添加点击事件
	$('button').eq(0).click(function(){
		$('[type=checkbox]').checkall()
	})
	//给全部选按钮添加点击事件
	$('button').eq(1).click(function(){
		$('[type=checkbox]').notall()
	})
	//给反选按钮添加点击事件
	$('button').eq(2).click(function(){
		$('[type=checkbox]').reserve()
	})
	//定义一个字符串
	var str = '    sjhdjshd sjidjs  kjs '
	//打印删除前面的空格之后的str
	console.log('---------'+$.rmspacefront(str)+'---------')
	//打印删除后面的空格后的str
	console.log('----'+$.rmspaceback(str)+'------')
	//打印删除前面和后面的空格
	console.log('-------'+$.rmspaceFandB(str)+'------')
	//打印删除所有的空格后的str
	console.log('------'+$.rmspaceall(str)+'------')
</script>

这是一个简单的jQuery的工具封装的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值