2017阿里前端测试题(div模拟下拉列表)
2017-03-15 21:49
337 查看
<!DOCTYPE html> <head> <meta charset="UTF-8"> <title>body</title> <style> #select{ border: 1px rgb(221,221,221) solid; width: 120px; height: 20px; text-align: center; } .selectNode{ position: relative; left: -1px; border: 1px rgb(221,221,221) solid; width: 120px; height: 20px; } .selectNode:hover{ background-color: rgb(221,221,221); cursor: default; } .cursor { animation: blink 1s infinite; position: relative; top: -2px; left:-58px; } @keyframes blink { 0%, 100% { color: black; } 50% { color: white; } } </style> </head> <body> <div id="select"></div> <script> function select(options){ // your code here //获取节点 var node = document.getElementById(options.srcNode.replace(/#/,"")); //点击事件 node.onclick = function(){ var cursor,span,selectNode,textNode,sNodeLen; //删除子元素 while(node.hasChildNodes()) { node.removeChild(node.firstChild); } //光标 cursor = document.createTextNode("|"); span = document.createElement("span"); span.appendChild(cursor); span.setAttribute("class", "cursor"); node.appendChild(span); //开始添加动态子元素并绑定事件 sNodeLen = document.getElementsByClassName("selectNode").length; if (sNodeLen == 0) { options.data.forEach(function(item){ textNode = document.createTextNode(item); selectNode = document.createElement("div"); selectNode.appendChild(textNode); selectNode.setAttribute("class", "selectNode"); node.appendChild(selectNode); //绑定事件 selectNode.onclick = function(e){ //阻止冒泡事件 e.stopPropagation(); while(node.hasChildNodes()) { node.removeChild(node.firstChild); } node.appendChild(document.createTextNode(this.firstChild.nodeValue)); } }); } }; } // eg select({ srcNode: '#select', data: ['北京','上海','杭州'], onChange: (ev)=>{ console.log(ev.value); } }) </script> </body> </html>
相关文章推荐
- 可编辑Select下拉列表控件实现方法(非DIV模拟)
- jquery用div模拟一个下拉列表框
- javascript 模拟select下拉列表特效
- 弹出DIV层的时候,把页面上的下拉列表全部隐藏.
- [置顶] 后台导航栏下拉列表 css+div 样式代码 及示例下载(灾情分享网)
- 用dl模拟实现可自定义样式的SELECT下拉列表(已封装)
- JS+DIV+CSS实现仿表单下拉列表效果
- jquery用div实现下拉列表的效果
- jQuery Validation Engine验证模拟的下拉列表非select
- 基于jquery的用dl模拟实现可自定义样式的SELECT下拉列表(已封装)
- 后台导航栏下拉列表 css+div 样式代码 及示例下载(灾情分享网)
- 可编辑的下拉列表+模糊搜索(模拟)
- 模拟一个类似百度google的模糊搜索下拉列表
- 下拉列表div
- js+div实现的下拉列表
- div模拟的下拉框特效
- DIV+CSS+JS下拉列表s实例
- jquery-div模拟下拉列表框(Select)插件
- 下拉列表被div高度隐藏的解决办法
- DIV盖住下拉列表