文本框(input)獲取焦點(onfocus)時樣式改變的示例代碼
摘要:許多重視用戶體驗的設(shè)計師都希望給文本框(input)加上獲取焦點或者鼠標(biāo)懸停時的樣式切換效果。其實很簡單,我們只需要獲取頁面上的文本框,加上onfocus事件或者其他對應(yīng)的事件即可。本文介紹了如何在獲取焦點時切換樣式,明白原理后,實現(xiàn)其他效果就很簡單了。
許多重視用戶體驗的設(shè)計師都希望給文本框(input)加上獲取焦點或者鼠標(biāo)懸停時的樣式切換效果。其實很簡單,我們只需要獲取頁面上的文本框,加上onfocus事件或者其他對應(yīng)的事件即可。本文介紹了如何在獲取焦點時切換樣式,明白原理后,實現(xiàn)其他效果就很簡單了。
function focusInput(focusClass, normalClass) {
var elements = document.getElementsByTagName("input");
for (var i=0; i < elements.length; i++) {
if (elements[i].type != "button" && elements[i].type != "submit" && elements[i].type != "reset") {
//if(elements[i].type=="text"){
elements[i].onfocus = function() { this.className = focusClass; };
elements[i].onblur = function() { this.className = normalClass||''; };
}
}
}
window.onload = function() {
focusInput('focusInput', 'normalInput');
}
<style type="text/css">
.normalInput { border:1px solid #ccc; }
.focusInput { border:1px solid #FFD42C; }
</style>
相關(guān)文章
用ASP將SQL搜索出來的內(nèi)容導(dǎo)出為TXT的代碼
用ASP將SQL搜索出來的內(nèi)容導(dǎo)出為TXT的代碼...2007-07-07JavaScript canvas實現(xiàn)圍繞旋轉(zhuǎn)動畫
這篇文章主要為大家詳細介紹了JavaScript canvas實現(xiàn)圍繞旋轉(zhuǎn)動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11js中字符替換函數(shù)String.replace()使用技巧
js中字符替換函數(shù)String.replace()使用技巧,字符替換經(jīng)常用的到。2011-08-08