关于js函数重构的问题

今天在走获得焦点动作时,想着去重构一下获得和失去焦点的代码,重构前:

$(".searchInput").focus(function(){
		$(".search").css("width","320px");
		$(".searchInput").css({"width":"268px","border":"none","outline":"none"});
		$(".searchHidden").css("display","block");
		$(".searchbtn").css({"background-color":"#969696","color":"white"});
	});
	$(".searchInput").blur(function(){
		$(".search").css("width","240px");
		$(".searchInput").css({"width":"188px","border-style":"none"});
		$(".searchHidden").css("display","none");
	});
重构后:

$(".searchInput").focus(function(){
		changeInputDiv("320px","268px","block","#969696","white")
	});
	$(".searchInput").blur(function(){
		changeInputDiv("240px","188px","none","#eeeeee","#969696")
	});
	
	function changeInputDiv(width1,width2,display,bakcolor,color){
		$(".search").css("width",width1);
		$(".searchInput").css("width",width2);
		$(".searchHidden").css("display",display);
		$(".searchbtn").css({"background-color":bakcolor,"color":color});
	}
遇到的问题是:刚开始我直接这样子写,没有在为onfocus建立一个函数,所以解析器是直接调用了changeInputDiv函数,并不能表现出获得焦点和失去焦点的变化。没有点击时就已经调用了这个函数,而获得焦点和失去时就不再有反应。
$(".searchInput").οnfοcus=changeInputDiv("320px","268px","block","#969696","white");




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值