为什么选取不到ul
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="css/index.css"> </head> <body> <div id="wrap"> <div> <div class="title">自定义select</div> <!-- <select id="demo"> <option value="1" type="构建工具">Babel</option> <option value="2" type="构建工具">Webpack</option> <option value="3" type="构建工具">Rollup</option> <option value="4" type="前端框架">Vue</option> <option value="5" type="前端框架">Angular</option> <option value="6" type="前端框架">React</option> <option value="7" type="前端框架">Nerv</option> </select> --> <div class='selector'> <div class='selector-main'> <input type='text' class="selector-content"></input> <div class="arow-btn">▼</div> </div> <div class="selector-item"> <input type="text" class='search-bar' placeholder="搜索"> <div class="search-list"></div> </div> </div> </div> <div> <p id="out"></p> </div> <!-- <ul> <div>title</div> <li>1</li> <li>2</li> <div>tittle</div> <li>3</li> </ul> --> <div style="text-align: center;margin-top: 100px;"> <div class="title">原生select</div> <select> <option value="1" type="构建工具">Babel</option> <option value="2" type="构建工具">Webpack</option> <option value="3" type="构建工具">Rollup</option> <option value="4" type="前端框架">Vue</option> <option value="5" type="前端框架">Angular</option> <option value="6" type="前端框架">React</option> <option value="7" type="前端框架">Nerv</option> </select> </div> </div> <script type="text/javascript"src='js/index.js'></script> <script type="text/javascript"src='js/data.js'></script> <script> const selector=new $selector({ data, parasitifer:'.search-list' }) </script> </body> </html>
*{ padding:0; margin:0; list-style: none; } body{ background-color: #ddd; } #wrap{ margin-top:300px; } .title{ color:lightblue; font-weight: bold; font-size:16px; display: -webkit-flex; display: -moz-flex; display: -ms-flex; display: -o-flex; display: flex; justify-content: center; } .selector-main{ display: -webkit-flex; display: -moz-flex; display: -ms-flex; display: -o-flex; display: flex; justify-content: center; } .selector{ display: -webkit-flex; display: -moz-flex; display: -ms-flex; display: -o-flex; display: flex; flex-direction:column; justify-content: center; align-items: center; position: relative; } .selector-item{ width:194px; opacity: 0; background-color: #fff; overflow: auto; max-height: 200px; transition: all .3s; position: absolute; top:21px; } .arow-btn{ color:white; width:21px; height:21px; background-color: lightblue; text-align: center; cursor: pointer; } .selector-title{ font-weight: bold; font-size:20px; } li{ padding: 5px 0 5px 10px; } li:hover{ background-color: #ddd; }
(function(window,document){ const co={ appendChild(parent,...element){ element.forEach(el=>{ parent.appendChild(el); }); }, $(element,root=document){ return root.querySelector(element); }, $$(element,root=document){ return root.querySelectorAll(element); }, } let Selector=function(options){ this._init(options); this._bind(); this._list(); this._content(); } Selector.prototype._init=function({data,parasitifer}){ this.parasitifer=co.$(parasitifer);//挂载 this.type=['构建工具','前端框架'];//所有的分类 this.classified={'构建工具':[],'前端框架':[]};//分类好的数据 this.classify(data); this.shown=false; console.log(this.classified); } //分类 Selector.prototype.classify=function(data){ data.forEach(({type,value,number})=>{ if(this.type.includes(type)){ this.classified[type].push(value); } }) } //生成下拉列表 Selector.prototype._list=function(){ let ul=document.createElement('ul'); let ulContent=''; for(let i in this.classified){ let div=`<div class='selector-title'>${i}</div>`; let li=''; for(let k in this.classified[i]){ li+=`<li>${this.classified[i][k]}</li>` } ulContent+=div+li; } ul.innerHTML=ulContent; co.appendChild(co.$('.search-list'),ul); } Selector.prototype.show=function(ele){ if(!this.shown){ ele.style.opacity=1; setTimeout(()=>{ ele.style.display='block' this.shown=true; },300); }else{ ele.style.opacity=0; setTimeout(()=>{ ele.style.display='none' this.shown=false; },300); } } Selector.prototype._bind=function(){ let arrow=co.$('.arow-btn'); let item=co.$('.selector-item'); //点击箭头收放下拉列表 arrow.addEventListener('click',(e)=>{ e.stopPropagation(); if(!this.shown){ this.show(item); }else{ this.show(item); } }); // 点击其他区域收起列表 document.addEventListener('click',()=>{ if(this.shown){ this.show(item); } }) // 点击列表项 let list=co.$('.search-list'); let ul=co.$('ul',list); console.log(ul); } Selector.prototype._content=function(){ } window.$selector=Selector; })(window,document)
怎么选取都选取不到,这是为什么啊啊啊
12
收起
正在回答
2回答
同学你好,把list这个方法的调用,放到bind之前就可以了哦。因为list这个是列表生成的方法。但是bing中有调用show这个方法,在调用的时候,列表还没生成,所以获取不到哦。
希望能帮助到你,欢迎采纳。
祝学习愉快!
4.Vue与React高级框架开发
- 参与学习 人
- 提交作业 239 份
- 解答问题 10739 个
本阶段带你深入前端开发的肌理,通过ES6基础知识和前端主流高级框架的学习,助你快速构建企业级移动webAPP应用,进入职场的终极battle
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星