2013-10-21 32 views
1

即時製作一個好看的登錄系統,所以我想在表單中輸入內容時改變文本的顏色,但即時通訊無法弄清楚如何,我給他們分配了一個類來做這項工作,但它似乎沒有工作。用if語句指定類HTML

HTML: -

<div class="Login"> 
       <form name="form1" method="get" action="index.php"> 
       <input type="text" value="Username" name="Username" onblur="if (this.value === '') {this.value = 'Username'; this.class='Offline';}" onfocus="if (this.value === 'Username') {this.value = ''; this.class='Online';}"> 
       <input type="text" value="Password" name="Password" onblur="if (this.value === '') {this.value = 'Password';}" onfocus="if (this.value === 'Password') {this.value = '';}"> 
       <input type= "Submit" Name= "Submit" value= "Login" class="Button"> 
       </form> 
    </div> 

和IM改變對這些類在CSS的顏色。但所行似乎工作,任何人都可以指導我在這裏,我沒有很多的JavaScript腳本的經驗,所以我不想去那邊

回答

0

使用this.className代替this.class

<input type="text" value="Username" name="Username" onblur="if (this.value === '') {this.value = 'Username'; this.className='Offline'}" onfocus="if (this.value === 'Username') {this.value = ''; this.className='Online';}"> 

Example

更新

你必須改變條件,隨着病情並不令人滿意,並沒有應用類:

<input type="text" value="Username" name="Username" onblur="if (this.value != '') <-- Here {this.value = 'Username'; this.className='Offline'}" onfocus="if (this.value === 'Username') {this.value = ''; this.className='Online';}"> 

Updated Fiddle

+0

它似乎正在工作,當我改變背景,我該如何改變字體顏色 –

+0

@SahilSingh只使用'color:...'而不是'background-color' –

+0

顏色不會像那樣改變一些原因,即時通訊能夠做的一切與字體,但改變顏色 –

0

使用this.className而不是this.class

<input type="text" value="Username" name="Username" onblur="if (this.value === '') {this.value = 'Username'; this.className='Offline';}" onfocus="if (this.value === 'Username') {this.value = ''; this.className='Online';}"> 

JsFiddle Demo

0

你試過與JQuery的?

$(this).attr('class','yourclass'); 

$(this).removeClass('Offline'); 
$(this).addClass('Online'); 
0

您應該使用的setAttribute和的getAttribute方法,而不是類或類名的屬性。