當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML autofocus屬性用法及代碼示例


HTML中的autofocus屬性用於指定元素在頁麵加載時應該獲得焦點。這是一個布爾屬性。

注意:它僅支持以下元素:<按鈕>,<輸入>,<選擇>和<文本區域>

用法:

<elementName autofocus>

範例1:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>HTML autofocus Attribute</title> 
    </head> 
    <body style = "text-align:center;"> 
        <h1 style = "color:green;">GeeksforGeeks</h1> 
        <h2>HTML autofocus Attribute</h2> 
        <form> 
            <label>First Name:
                    <input type="text" autofocus  
                            placeholder = "Enter Name"> 
            </label>           
        </form> 
    </body> 
</html>

輸出:
autofocus



範例2:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>HTML autofocus Attribute</title> 
    </head> 
    <body style = "text-align:center;"> 
        <h1 style = "color:green;">GeeksforGeeks</h1> 
        <h2>HTML autofocus Attribute</h2> 
       <textarea rows="3" cols="30" autofocus> 
            A computer science portal for geeks. 
        </textarea> 
    </body> 
</html>

輸出:
autofocus

支持的瀏覽器:下麵列出了autofocus屬性支持的瀏覽器:

  • 穀歌瀏覽器5.0
  • Internet Explorer 10.0
  • Firefox 4.0
  • Opera 9.6
  • 蘋果Safari 5.0




相關用法


注:本文由純淨天空篩選整理自Vishal Chaudhary 2大神的英文原創作品 HTML | autofocus Attribute。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。