Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> HTML

คุณสมบัติ HTML DOM nodeValue


คุณสมบัติ HTML DOM nodeValue ส่งคืน/ตั้งค่าสตริงที่สอดคล้องกับค่าของโหนด (node)

ไวยากรณ์ (Syntax)

การส่งคืนค่าสตริง:

Node.nodeValue

ที่นี่ ค่าที่ส่งคืนอาจเป็นดังต่อไปนี้:

  • ค่า 'null' สำหรับ document nodes และ element nodes
  • ค่า 'value' ของแอตทริบิวต์สำหรับ attribute nodes
  • ค่าเนื้อหาสำหรับ text nodes และ comment nodes

การตั้งค่า nodeValue เป็นค่าสตริง:

Node.nodeValue = string

หมายเหตุ: ช่องว่าง (whitespaces) ถูกนับเป็น text nodes เท่านั้น

ตัวอย่างการใช้งาน

<!DOCTYPE html> <html> <head> <title>HTML DOM nodeValue</title> <style> 
    form { 
        width: 70%; 
        margin: 0 auto; 
        text-align: center; 
    } 
    * { 
        padding: 2px; 
        margin: 5px; 
    } 
    input[type="button"] { 
        border-radius: 10px; 
    } 
    ul { 
        width: 30%; 
        margin: 0 auto; 
    } 
</style> </head> <body> 
    <form> 
        <fieldset> 
            <legend>HTML-DOM-nodeValue</legend> 
            <h3>Students</h3> 
            <ul> 
                <li>Adam</li> 
                <li>Alex</li> 
                <li>Bina</li> 
                <li>Eden</li> 
                <li>Rajesh</li> 
                <li>Zampa</li> 
            </ul> 
            <input type="button" onclick="checkForBina()" value="Confirm for Bina"> 
            <div id="divDisplay"></div> 
        </fieldset> 
    </form> 
    <script> 
        var divDisplay = document.getElementById("divDisplay"); 
        var studentList = document.getElementsByTagName("li"); 
        var status = 'not Present'; 
        function checkForBina() { 
            for(var i = 0; i < studentList.length; i++) { 
                if(studentList[i].childNodes[0].nodeValue === 'Bina') 
                    status = 'Present'; 
            } 
            divDisplay.textContent = 'Bina is ' + status; 
        } 
    </script> 
</body> </html>

ผลลัพธ์

ก่อนคลิก 'Confirm for Bina':

คุณสมบัติ HTML DOM nodeValue

หลังคลิก 'Confirm for Bina':

คุณสมบัติ HTML DOM nodeValue