亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這段代碼可以優化嗎?可以的話怎么優化

這段代碼可以優化嗎?可以的話怎么優化

hhhzihao2 2017-02-24 10:07:39
onload?=?function(){ ????/*?聚焦時隱藏placeholder,失焦時顯示?*/ ????var?Inputs?=?document.getElementsByTagName("input"),Textareas?=?document.getElementsByTagName("textarea"); ????for?(?var?i?in?Inputs?)?{ ????????Inputs[i].onfocus?=?function(){ ????????????this.placeholder_str?=?this.placeholder???this.placeholder?:?''; ????????????this.placeholder?=?''; ????????}; ????????Inputs[i].onblur?=?function(){ ????????????this.placeholder?=?this.placeholder_str???this.placeholder_str?:?''; ????????} ????} ????for?(?var?i?in?Textareas?)?{ ????????Textareas[i].onfocus?=?function(){ ????????????this.placeholder_str?=?this.placeholder???this.placeholder?:?''; ????????????this.placeholder?=?''; ????????}; ????????Textareas[i].onblur?=?function(){ ????????????this.placeholder?=?this.placeholder_str???this.placeholder_str?:?''; ????????} ????} };
查看完整描述

3 回答

?
依然c

TA貢獻54條經驗 獲得超32個贊

聚焦隱藏placeholder這個html5里面就有啊。<input type="text" placeholder="hello world">.

但是如果你是想寫的兼容的話,js里面循環最好不要用for in,除非你不知道這個數組的長度,一本優化書里說的。

而且你們document.get...這個,沒寫一次都要查找一次的,定義好再用,比如let textarea = doc... ?然后再用

查看完整回答
1 反對 回復 2017-02-24
  • hhhzihao2
    hhhzihao2
    1、聚焦隱藏placeholder這個html5里面就有?我用的谷歌和火狐都沒有哦 2、求代碼 嘿
  • 依然c
    依然c
    有啊,后面我貼出來了啊,不然我寫那個干嘛。就是input的那個,不用想太多,就這么簡單,試下就知道。還有,給個采納回復啊
  • hhhzihao2
    hhhzihao2
    有placeholder但聚焦不會隱藏
點擊展開后面1
?
hhhzihao2

TA貢獻8條經驗 獲得超1個贊

var?Inputs?=?document.getElementsByTagName('input');
var?Textareas?=?document.getElementsByTagName('textarea');
for?(var?i?=?0,?len?=?Inputs.length?+?Textareas.length;?i?<?len;?i++)?{
	var?Elem?=?i?<?Inputs.length???Inputs[i]?:?Textareas[i?-?Inputs.length];
	Elem.onfocus?=?function()?{
		this.placeholder_str?=?this.placeholder???this.placeholder?:?'';
		this.placeholder?=?''
	};
	Elem.onblur?=?function()?{
		this.placeholder?=?this.placeholder_str???this.placeholder_str?:?''
	}
}


查看完整回答
反對 回復 2017-02-27
?
瘋子520520

TA貢獻39條經驗 獲得超236個贊

可以的,可以將inputs及textareas合并一起處理,不用分開

查看完整回答
反對 回復 2017-02-24
  • 3 回答
  • 0 關注
  • 2124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號