Thuộc tính HTML DOM nodeValue trả về / đặt một chuỗi tương ứng với giá trị của nút.
Sau đây là cú pháp -
Trả về giá trị chuỗi
Node.nodeValue
Ở đây, giá trị trả về có thể như sau -
- Giá trị là 'null' cho các nút tài liệu và nút phần tử
- Giá trị là "giá trị" của thuộc tính cho các nút thuộc tính
- Giá trị như nội dung cho các nút văn bản và nút nhận xét
Đặt nodeValue thành một giá trị chuỗi
Node.nodeValue = string
LƯU Ý:Khoảng trắng chỉ được coi là các nút văn bản.
Hãy để chúng tôi xem một ví dụ về HTML DOM nodeValue tài sản -
Ví dụ
<!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> Đầu ra
Trước khi nhấp vào ‘Xác nhận cho Bina Nút '-
Sau khi nhấp vào ‘ Xác nhận cho Bina Nút '-