HTML DOM nodeValue 属性
htmlweb developmentfront end technology更新于 2024/3/19 0:12:17
HTML DOM nodeValue 属性返回/设置与节点值对应的字符串。
以下是语法 −
返回字符串值
Node.nodeValue
此处,返回值可以是以下 −
- 对于文档节点和元素节点,值为 ‘null’
- 值为 ‘value’属性节点的属性
- 将值作为文本节点和注释节点的内容
将 nodeValue 设置为字符串值
Node.nodeValue = string
注意:空格仅被视为文本节点。
让我们看一个 HTML DOM nodeValue 属性的示例 −
示例
<!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>
输出
点击"确认 Bina"按钮前 −

点击确认 Bina"按钮后 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

