|
楼主 |
发表于 2024-10-19 22:34
|
显示全部楼层
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>折叠文档搜索练习2</title>
</head>
<body>
<button type="button" id="toggl">启用输入框</button>
<input type="text" id="search" placeholder="...请输入欲搜索的姓名或电话的部份或全部..." style="width: 275px;" disabled>
<ul>
<details name="only1" class="only1"><summary>峡口支行</summary>
<li>林某 1234566</li>
<li>陆峰 222223</li>
<li>姜是辣 1234567</li>
</details>
<details name="only1" class="only1"><summary>石门支行</summary>
<li>吴云 4911228</li>
<li>郑燕 4990020</li>
<ol>
<details name="only2" class="only2"><summary>二级标题</summary>
<li>我是谁</li>
</details>
</ol>
<li>姜是辣 1234568</li>
</details>
</ul>
<script src="script.js"></script>
</body>
<script>
let idb=document.getElementById('toggl');
idb.addEventListener('click',myf1,false);
function myf1(){
if(idb.innerHTML=='启用输入框'){
document.getElementById('toggl').innerHTML='禁用输入框';
idb.innerHTML=document.getElementById('toggl').innerHTML;
document.querySelectorAll('details').forEach(details=>{details.removeAttribute('name')});
document.getElementById('search').disabled=false;
document.getElementById('search').focus();
document.querySelectorAll('details').forEach(details=>{details.open=false});
}
else{
document.getElementById('toggl').innerHTML='启用输入框';
idb.innerHTML=document.getElementById('toggl').innerHTML;
document.querySelectorAll('.only1').forEach(details=>{details.setAttribute('name','only1')});
document.querySelectorAll('.only2').forEach(details=>{details.setAttribute('name','only2')});
document.querySelectorAll('details').forEach(details=>{details.open=false});
document.getElementById('search').disabled=true;
}
};
document.getElementById('search').addEventListener('keyup',myf2,false);
function myf2(){
document.querySelectorAll('details').forEach(details=>{details.open=false});
const searcht=this.value.trim().toLowerCase();
const lis=document.querySelectorAll('li');
if(searcht === ''){return;}
lis.forEach(li => {
if(li.textContent.toLowerCase().includes(searcht)){
let paren=li.closest('details');
if(paren){
paren.closest('details').open=true;
paren.open=true;
}
/*while(paren){paren.open=true;paren=paren.closest('details');}*/
}
});
}
</script>
</html>
|
|