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


HTML value屬性用法及代碼示例


HTML中的value屬性用於指定與其一起使用的元素的值。對於不同的HTML元素,它具有不同的含義。用法:它可以與以下元素一起使用:<input>,<button>,<meter>,<li>,<option> <progress>和<param>。

    <輸入>:當value屬性存在時,它指定輸入元素的初始值。
    對於不同的輸入類型,它具有不同的含義:

    • 當出現在“button”,“reset”和“submit”中時,它指定按鈕上的文本。
    • 當出現在“text”,“password”和“hidden”中時,它指定輸入字段的初始值。
    • 當出現在“checkbox”,“radio”和“image”中時,它指定與輸入關聯的值。
  • 用法:
    <input value = "value">
  • 示例1:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>HTML value Attribute</title> 
        </head> 
        <body style = "text-align:center"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                HTML value Attribute 
            </h2> 
              
            Input:<input type = "text" value = "GeeksforGeeks"> 
      
        </body> 
    </html>
  • Output:
    valueinput
  • 示例-2:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>HTML value Attribute</title> 
        </head> 
        <body style = "text-align:center"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                HTML value Attribute 
            </h2> 
              
            <input type = "button" value = "Click me!"> 
      
        </body> 
    </html>
  • Output:
    inputvalue
  • <button>:存在value屬性時,它指定button元素的初始值。

  • 用法:
    <button value = "value">
  • Example:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>HTML value Attribute</title> 
        </head> 
        <body style = "text-align:center"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                HTML value Attribute 
            </h2> 
              
            <button id="btn" value="GeeksforGeeks" onclick="geek()"> 
             Click me!</button> 
              
            <p id="g"></p> 
              
            <script> 
                function geek() { 
                  var x = document.getElementById("btn").value; 
                  document.getElementById("g").innerHTML = "Welcome to " + x; 
                } 
            </script> 
        </body> 
    </html>
  • Output:
    在單擊按鈕之前:
    valuebutton
    單擊按鈕後:
    valuebutton
  • <meter>:指定量規的當前值。該值必須介於min和max屬性之間。

  • 用法:
    <meter value = "value">
  • Example:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>HTML value Attribute</title> 
        </head> 
        <body style = "text-align:center"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                HTML value Attribute 
            </h2> 
              
            <p>Health:<meter min="0" low="40" high="90"
                              max="100" value="60"></meter></p> 
        </body> 
    </html>
  • Output:
    valuemeter
  • <li>:存在value屬性時,它指定列表項的初始值。它僅適用於排序清單。



  • 用法:
    <li value = "number">list item </li>
    • number:指定列表項的值。
  • Example:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>HTML value Attribute</title> 
        </head> 
        <body > 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                HTML value Attribute 
            </h2> 
              
            <p>Sorting Algorithms</p> 
              
            <ol> 
              <li value="50">Merge sort</li> 
              <li>Quick sort</li> 
              <li>Insertion sort</li> 
            </ol> 
        </body> 
    </html>
  • Output:
    valuelist
  • <option>:存在value屬性時,它指定option元素的值。

  • 用法:
    <option value = "value"></option>
  • Example:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>HTML value Attribute</title> 
        </head> 
        <body style = "text-align:center"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                HTML value Attribute 
            </h2> 
              
            Sorting Algorithms:
            <select id="opt"> 
              <option value="quick">Quick sort</option> 
              <option value="merge">Merge sort</option> 
              <option value="insertion">Insertion sort</option> 
            </select> 
              
            <button type="button" onclick="geek()">Click me!</button> 
            <p id = "p"></p> 
            <script> 
                function geek() { 
                  var x = document.getElementById("opt").selectedIndex; 
                  var y = document.getElementsByTagName("option")[x].value; 
                  document.getElementById("p").innerHTML = "The selected  
                  option has value equals " + y + "."; 
                } 
            </script> 
        </body> 
    </html>
  • Output:
    在單擊按鈕之前:
    valueoption
    單擊按鈕後:
    valueoption
  • <progress>:存在value屬性時,它指定progress元素的值。

  • 用法:
    <progress value = "number"></progress>
    • 數字指定進度元素的初始值。
  • Example:
    <!DOCTYPE html> 
    <html> 
        <head> 
            <title>HTML value Attribute</title> 
        </head> 
        <body style = "text-align:center"> 
            <h1 style = "color:green;"> 
                GeeksforGeeks 
            </h1> 
              
            <h2> 
                HTML value Attribute 
            </h2> 
              
            Progress:
            <progress value="65" max="100"> 
            </progress> 
        </body> 
    </html>
  • Output:
    valueprogress
  • 支持的瀏覽器:進度元素中的value屬性支持的瀏覽器如下:

    • 穀歌瀏覽器
    • IE瀏覽器
    • Firefox
    • Opera
    • Safari

相關用法


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