一个空间是一个孩子。因此,如果您之间有空格,则无法获得。
一种方法是编写没有空格的代码并使用 firstChild
<div class="pdf-icon-box" style="position:relative;" onmouseover="this.firstChild.firstChild.style.backgroundColor = 'yellow'"; onmouseout="this.firstChild.firstChild.style.backgroundColor = 'green'"; ><span class="pdf-style"><span class="name" style="display:inline-block;"> T E S T </span></span></div>
第二种方法是再次使用 childNodes[childelement] :空格/制表符/新行是一个孩子
<div class="pdf-icon-box" style="position:relative;" onmouseover="this.childNodes[0].childNodes[0].style.backgroundColor = 'yellow'"; onmouseout="this.childNodes[0].childNodes[0].style.backgroundColor = 'green'"; ><span class="pdf-style"><span class="name" style="display:inline-block;"> T E S T </span></span></div>
第三种方法是使用className
<div class="pdf-icon-box" style="position:relative;" onmouseover="this.getElementsByClassName('name')[0].style.backgroundColor = 'yellow'"; onmouseout="this.getElementsByClassName('name')[0].style.backgroundColor = 'green'"; >
<span class="pdf-style">
<span class="name" style="display:inline-block;"> T E S T </span>
</span>
</div>
但这不是一个好方法。使用 CSS
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<style>
.name{background-color:yellow;}
.name:hover{background-color:green;}
</style>
<body>
<div class="pdf-icon-box" style="position:relative;">
<span class="pdf-style">
<span class="name"> T E S T </span>
</span>
</div>
</body>
</html>