1. 程式人生 > >用JS新增聚焦事件,以給父元素新增邊框陰影為例

用JS新增聚焦事件,以給父元素新增邊框陰影為例

function setBoxShadow (){
	$('.class').each(function () {
		$(this).focus(function(){
			$(this).parent().css("outline","none");/*禁用瀏覽器自帶邊框陰影*/
			$(this).parent().css("box-shadow","0px 0px 15px 1.5px #95B8E7");
		});
		$(this).blur(function(){
			$(this).parent().css("box-shadow","none");
		});
	});
}

這段程式碼給我們選中的元素都添加了聚焦和失焦事件,

聚焦時給父元素新增邊框陰影,失焦時去除陰影。