jquery事件二 -- 选项卡,失去焦点

以之前的选项卡例子为原版,当选上某一个选项卡的时候,选项卡周围会有一个蓝色的边框影响视觉体验,那么应该怎么去掉这个边框色呢?只需要加一行blur()--失去焦点函数就可以了

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>	
	<style type="text/css">
		.btns{
			500px;
			height:50px;
		}

		.btns input{
			100px;
			height:50px;
			background-color:#ddd;
			color:#666;
			border:0px;
		}

		.btns input.cur{
			background-color:gold;
		}


		.contents div{
			500px;
			height:300px;
			background-color: gold;
			display:none;
			line-height:300px;
			text-align:center;
		}

		.contents div.active{
			display: block;
		}



	</style>
	<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript">
		$(function(){

			$('#btns input').click(function() {

				$(this).blur(); //注意blur()的用法,加上这个就没边框色了

				// this是原生的对象
				$(this).addClass('cur').siblings().removeClass('cur');

				//$(this).index() 获取当前按钮所在层级范围的索引值
				$('#contents div').eq($(this).index()).addClass('active').siblings().removeClass('active');

			});
		})
		
	</script>
</head>
<body>
	<div class="btns" id="btns">
		<input type="button" value="tab01" class="cur">
		<input type="button" value="tab02">
		<input type="button" value="tab03">
	</div>

	<div class="contents" id="contents">
		<div class="active">tab文字内容一</div>
		<div>tab文字内容二</div>
		<div>tab文字内容三</div>
	</div>
</body>
</html>

  

 二. input框事件

有三个关于焦点的事件函数常用于input框中,分别如下

blur() 元素失去焦点

focus() 元素获得焦点

change() 表单元素的值发生变化

例子如下,用一个简单的input框做实验,并且测试了keyup()

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
	<script type="text/javascript">
		$(function(){

			//$('#txt01').focus();  这里的意义是一开始就获得光标

			// 获取焦点的时候-也就是鼠标点input框会有光标出现,这里测试用弹出foucus框
			$('#txt01').focus(function() {
				//alert('foucus');
			});

			// 失去焦点-就是鼠标点击input框之外时,失去光标的情况。测试用弹出blur
			$('#txt01').blur(function() {
				//alert('blur');
			});


			/*
			输入框内的内容发生变化,并且失去焦点后触发
			也就是input框里输入内容后,并且点击input框之外的地方后会弹出change框,然后还会弹出上面的blur框
			*/

			$('#txt01').change(function() {
				alert('change');
			});
			


			//按键松开后就触发,也就是输入inpu中的内容松开键盘的时候就会弹出内容
            //触发频率太高,每输入一个字母就会弹出框
			/*

			$('#txt01').keyup(function() {
				alert('keyup');
			});

			*/






		})

	</script>
</head>
<body>
	<input type="text" name="" id="txt01">
</body>
</html>

  

原文地址:https://www.cnblogs.com/regit/p/9003226.html