JS树形结构筛选和折叠
- 效果图
2.筛选
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>树结构</title>
<style>
ul.tree, ul.tree ul {
list-style-type: none;
background-color: red;
margin: 0;
padding: 0;
}
ul.tree ul {
margin-left: 10px;
}
ul.tree li {
margin: 0;
padding: 0 12px;
line-height: 20px;
background-color: pink;
color: #369;
font-weight: bold;
}
ul.tree li:last-child {
background-color: yellow;
}
</style>
<script>
function search(){
var treedom = document.getElementById("treeDom")
var li = treedom.getElementsByTagName("li")
var searchname = document.getElementById("inputsearch").value;
if (searchname.length>0){
for( var i = 0 ;i< li.length ; i++){
if(li[i].innerHTML.toUpperCase().indexOf(searchname)>-1){
li[i].style.display="block";
if( li[i].parentElement){
li[i].parentElement.style.display="block"
}
}else{
li[i].style.display="none";
if( li[i].parentElement&& li[i].parentElement.style.display !="block"){
li[i].parentElement.style.<