简体   繁体   中英

onblur and onfocus Help - Javascript function alternative

I would like to know how i would replace my onblur / onfocus HTML code with a Javascript function and also perform the same technique to a select option.

Code example:

<table>

<tr><td>

 <label  for="firstname">*Firstname:</label>
     <input type="text" name="udetail" id="firstname" value="(e.g. John)" 
     onblur="if(this.value == '') { this.style.color='#999'; this.value='(e.g. John)'} 
     {this.style.border='1px #bbb solid'}" onfocus="if (this.value == '(e.g. John)') 
     {this.style.color='#000'; this.value=''}" tabindex="1" />

 </td></tr>



  <tr><td>

     <label for="where">*How did you hear about us:</label>
 <select size="1" name="udetail" id="where" class="input"  
     tabindex="7" />
                                        <option value="blank">Select</option>                        
                                        <option value="ampdj">AMPdj</option>
                                        <option value="bing">Bing</option>
                                        <option value="google">Google</option>                  
                                        <option value="pastfunction">Past function</option>
                                        <option value="recomended">Recomended</option>
                                        <option value="yell">Yell</option>
                                        <option value="other">Other</option>                                            
  </select>                                                                                                                                    

  </td></tr>                      

  </table>

Thanks, Scott

如果不严格要求您在此类次要ui元素中支持旧版浏览器,则建议您使用输入标签的HTML5占位符属性:

<input type="text" placeholder="(e.g. John)" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM