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


HTML DOM Output用法及代码示例

HTML DOM 输出对象用于表示 HTNL <Output> 标签。输出元素由 getElementById() 访问。

用法:

document.getElementById("id");

其中 “id” 是分配给“输出”标签的 ID。



值:

  • default Value:它用于设置或返回 <output> 元素的默认值。
  • form:  它返回对 <output> 元素所属表单的引用。
  • htmlFor:它用于返回 <output> 元素的属性的值。
  • labels: 它用于返回与 <output> 元素关联的标签元素列表。
  • name:它用于设置或返回 <output> 元素的 name 属性的值。
  • 类型:它返回 Output 对象代表的 HTML 元素类型。
  • value:它用于设置或返回 <output> 元素的 value 属性的值。

范例1:下面的 HTML 代码返回输出元素的值。通过使用 getElementById() 方法访问输出标签。

HTML


<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML DOM Output Object
    </title>
    <style>
        body {
            text-align:center;
        }
  
        h1 {
            color:green;
        }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
    <h2>
        HTML DOM Output Object
    </h2>
    <form oninput="sumresult.value = parseInt(A.value)
                + parseInt(B.value) + parseInt(C.value)">
        <input type="number" name="A" value="50" /> +
  
        <input type="range" name="B" value="0" /> +
  
        <input type="number" name="C" value="50" />
        <br /> Submit Result:
        <output name="sumresult" id="geeks" for="A B C">
        </output>
        <br>
        <br>
    </form>
    <Button onclick="myGeeks()">Submit</Button>
    <h2 id="sudo"></h2>
  
    <script>
        function myGeeks() {
            var x = document.getElementById("geeks").value;
            document.getElementById("sudo").innerHTML = x;
        }
    </script>
  
</body>
  
</html>

输出:

范例2:可以使用 document.createElement 方法创建输出对象。

HTML


<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h2 style="color:green;">
            GeeksForGeeks
        </h2>
        <h2 style="color:green;">
            HTML DOM Output Object
        </h2>
  
        <p>
            Click the Beliw button to 
            create an OUTPUT element.
        </p>
  
        <form id="GFG" oninput=
            "x.value=parseInt(a.value)+parseInt(b.value)">0
            <input type="range" id="a" value="50">100
            +<input type="number" id="b" value="50">=
        </form>
        <br>
  
        <button onclick="Create()">Try it</button>
  
        <p id="sudo"></p>
    </center>
  
    <script>
        function Create() {
  
            // Create an output tag
            var x = document.createElement("OUTPUT");
            x.setAttribute("name", "x");  // Set name attribute
            x.setAttribute("for", "a b"); // Set firm attribute 
            document.getElementById("GFG").appendChild(x);
            document.getElementById("sudo").innerHTML =
                `The output element was created. You can 
                Change the value of the range or number 
                field to see the result of a calculation.`;
        }
    </script>
</body>
  
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自ManasChhabra2大神的英文原创作品 HTML DOM Output Object。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。