这个筛选的问题好多出错了
(function(window,document){
//定义一个方法
const methods = {
appendChild(parent,...children){
children.forEach(el=>{
parent.appendChild(el);
})
},
$(selector,root=document){
return root.querySelector(selector);
},
$$(selector,root=document){
return root.querySelectorAll(selector);
}
}
//D
let D = function(option){
//事件
this._ck();
this.el = methods.$('.sel');
this.lia = methods.$$('li',this.el);
this.lis();
this.bod = methods.$('body');
this._non(this.el);
this.sear();
this.search();
}
//搜索框点击事件
D.prototype._ck = function(){
methods.$('.symbol').addEventListener('click',(e)=>{
e.stopPropagation();
if(this.el.style.display!=="block"){
this.el.style.display="block";
}else{
this.el.style.display="none";
}
})
}
//点击空白消失
D.prototype._non = function(e){
document.onclick = function () {
e.style.display = "none";
}
}
//选择Li的值给输入框
D.prototype.lis = function(){
//遍历Li
var that = this;
that.inl = methods.$('.searcT');
that.input = methods.$('.search');
that.ser = methods.$('.sel');
that.elt = [];
that.inl.onclick = function(e){
e.stopPropagation();
}
this.lia.forEach((el,index)=>{
el.onclick = function(){
that.input.value = el.innerText;
that.ser.style.display="none";
}
that.elt.push(el.innerText);
})
}
//筛选搜索
D.prototype.sear = function(){
this.inl.addEventListener('input',()=>{
this.search(this.inl.value,[...this.elt]);
})
}
D.prototype.search = function(key,items){
return items.filter(item => {
if(item.includes(key)){
return item;
}
})
}
window.$D = D;
})(window,document)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定义下拉框</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="container">
<div class="title">自定义下拉框</div>
<div class="box">
<input type="text" class="search" placeholder="未选择" disabled >
<div class="symbol">∨</div>
<div class="sel">
<input type="text" class="searcT" placeholder="搜索" >
<b>构建工具</b>
<ul>
<li>Babel</li>
<li>Webpack</li>
<li>Rollup</li>
</ul>
<b>前端框架</b>
<ul>
<li>Vue</li>
<li>React</li>
<li>Augest</li>
</ul>
</div>
</div>
</div>
<script src="data.js"></script>
<script src="index.js"></script>
<script>
const d = new $D(
)
</script>
</body>
</html>
* {
margin: 0;
padding: 0;
list-style-type: none;
}
body{
width:100%;
height:100%;
}
.container {
position: relative;
text-align: center;
width:100%;
height:500px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 30px;
color: aqua;
}
.search {
border:1px solid #666;
margin-top: 30px;
width: 300px;
height: 30px;
outline: none;
border-radius: 5px;
padding-left:10px;
}
.box{
position: relative;
}
.symbol{
position: absolute;
width:50px;
height:32px;
right:0px;
top:30px;
background-color:blue;
color:white;
border-radius: 5px;
line-height: 34px;
cursor:pointer;
}
.sel{
position: absolute;
width:300px;
height:200px;
border:1px solid black;
border-radius: 5px;
overflow-y:scroll;
text-align: left;
display: none;
}
.searcT{
margin:1px 1px 1px 1px;
width:270px;
height:26px;
border-radius: 5px;
border:1px solid #666;
padding-left:5px;
}
.sel b{
margin-left: 5px;
}
.sel ul li{
font-size:14px;
text-indent: 20px;
}
.sel ul li:hover{
background-Color:red;
}
筛选功能哪里还需要修改吗
正在回答 回答被采纳积分+1
同学你好,是filter方法中定义的参数,代表的意思如下,可以参考文档:
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
参数名称自定义,不是固定的,但是每个位置参数代表的意思是固定的。
祝学习愉快!
(function(window,document){
//定义一个方法
const methods = {
appendChild(parent,...children){
children.forEach(el=>{
parent.appendChild(el);
})
},
$(selector,root=document){
return root.querySelector(selector);
},
$$(selector,root=document){
return root.querySelectorAll(selector);
}
}
//D
let D = function(option){
//事件
this._ck();
this.el = methods.$('.sel');
this.lia = methods.$$('li',this.el);
this.lis();
this.bod = methods.$('body');
this._non(this.el);
this.sear();
//this.search();
}
//搜索框点击事件
D.prototype._ck = function(){
methods.$('.symbol').addEventListener('click',(e)=>{
e.stopPropagation();
if(this.el.style.display!=="block"){
this.el.style.display="block";
}else{
this.el.style.display="none";
}
})
}
//点击空白消失
D.prototype._non = function(e){
document.onclick = function () {
e.style.display = "none";
}
}
//选择Li的值给输入框
D.prototype.lis = function(){
//遍历Li
var that = this;
that.inl = methods.$('.searcT');
that.input = methods.$('.search');
that.ser = methods.$('.sel');
that.elt = [];
that.inl.onclick = function(e){
e.stopPropagation();
}
this.lia.forEach((el,index)=>{
el.onclick = function(){
that.input.value = el.innerText;
that.ser.style.display="none";
}
that.elt.push(el.innerText);
})
}
//筛选搜索
D.prototype.sear = function(){
this.inl.addEventListener('input',()=>{
this.search(this.inl.value,[...this.elt]);
})
}
D.prototype.search = function(key,items){
for(var i=0;i<this.lia.length;i++){
this.lia[i].style.display="none";
}
items.filter((item,index)=>{
console.log(item)
this.lia[index].style.display="block";
})
}
window.$D = D;
})(window,document)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>自定义下拉框</title>
<link rel="stylesheet" href="./index.css">
</head>
<body>
<div class="container">
<div class="title">自定义下拉框</div>
<div class="box">
<input type="text" class="search" placeholder="未选择" disabled >
<div class="symbol">∨</div>
<div class="sel">
<input type="text" class="searcT" placeholder="搜索" >
<b>构建工具</b>
<ul>
<li>Babel</li>
<li>Webpack</li>
<li>Rollup</li>
</ul>
<b>前端框架</b>
<ul>
<li>Vue</li>
<li>React</li>
<li>Augest</li>
</ul>
</div>
</div>
</div>
<script src="data.js"></script>
<script src="index.js"></script>
<script>
const d = new $D(
)
</script>
</body>
</html>
* {
margin: 0;
padding: 0;
list-style-type: none;
}
body{
width:100%;
height:100%;
}
.container {
position: relative;
text-align: center;
width:100%;
height:500px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.title {
font-size: 30px;
color: aqua;
}
.search {
border:1px solid #666;
margin-top: 30px;
width: 300px;
height: 30px;
outline: none;
border-radius: 5px;
padding-left:10px;
}
.box{
position: relative;
}
.symbol{
position: absolute;
width:50px;
height:32px;
right:0px;
top:30px;
background-color:blue;
color:white;
border-radius: 5px;
line-height: 34px;
cursor:pointer;
}
.sel{
position: absolute;
width:300px;
height:200px;
border:1px solid black;
border-radius: 5px;
overflow-y:scroll;
text-align: left;
display: none;
}
.searcT{
margin:1px 1px 1px 1px;
width:270px;
height:26px;
border-radius: 5px;
border:1px solid #666;
padding-left:5px;
}
.sel b{
margin-left: 5px;
}
.sel ul li{
font-size:14px;
text-indent: 20px;
}
.sel ul li:hover{
background-Color:red;
}
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星