当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。