結果你為button準備的focus效果可能會跑到其他input上,例如radiobox、checkbox上。
所以通常會看到有人會給出兩個class一個是button,一個是button_focus,onfocus的時候,把button替換成button_focus。
input.button {…}
input.button_focus {…}
當然它可以工作,對于一向只用一個class的人來說,甚至可以用不太嚴謹的方式,在onfocus的時候className+='_focus',在onblur的時候className.replace('_focus',''),這段代碼可以通用,而不必為button寫一遍,又為radio或者checkbox再寫一遍。但是總的來說,這恐怕不是一個好方法。例如button_focus不能復用button的樣式特性。如果你要復用,必須寫成:
input.button, input.button_focus {…}
input.button_focus {…}
每一處類似的情況都要記住這個寫法(且兩句順序不能顛倒)。
特別討厭的是,即使在另一個地方,你不想要focus效果,因此只需要input.button,而不需要input.button_focus,但你也要記得第一句的寫法,否則一旦有了焦點,input.button樣式就失效了!
文章來源于領測軟件測試網 http://www.kjueaiud.com/