时间:2021-05-26
本文实例讲述了JS+CSS实现美化的下拉列表框效果。分享给大家供大家参考。具体如下:
一款经过JS+CSS美化的下拉列表,效果很不错,但代码有点偏多,学习CSS的朋友可以学习一下方法,然后自己变通一下,把代码简化一下。
运行效果如下图所示:
具体代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http:///file_images/article/201508/2015811145849095.gif") no-repeat 0 -66px;color:#19555F}</style><script>var selects = document.getElementsByTagName('select');var isIE = (document.all && window.ActiveXObject && !window.opera) ? true : false;function $(id) { return document.getElementById(id);}function stopBubbling (ev) { ev.stopPropagation();}function rSelects() { for (i=0;i<selects.length;i++){ selects[i].style.display = 'none'; select_tag = document.createElement('div'); select_tag.id = 'select_' + selects[i].name; select_tag.className = 'select_box'; selects[i].parentNode.insertBefore(select_tag,selects[i]); select_info = document.createElement('div'); select_info.id = 'select_info_' + selects[i].name; select_info.className='tag_select'; select_info.style.cursor='pointer'; select_tag.appendChild(select_info); select_ul = document.createElement('ul'); select_ul.id = 'options_' + selects[i].name; select_ul.className = 'tag_options'; select_ul.style.position='absolute'; select_ul.style.display='none'; select_ul.style.zIndex='999'; select_tag.appendChild(select_ul); rOptions(i,selects[i].name); mouseSelects(selects[i].name); if (isIE){ selects[i].onclick = new Function("clickLabels3('"+selects[i].name+"');window.event.cancelBubble = true;"); } else if(!isIE){ selects[i].onclick = new Function("clickLabels3('"+selects[i].name+"')"); selects[i].addEventListener("click", stopBubbling, false); } }}function rOptions(i, name) { var options = selects[i].getElementsByTagName('option'); var options_ul = 'options_' + name; for (n=0;n<selects[i].options.length;n++){ option_li = document.createElement('li'); option_li.style.cursor='pointer'; option_li.className='open'; $(options_ul).appendChild(option_li); option_text = document.createTextNode(selects[i].options[n].text); option_li.appendChild(option_text); option_selected = selects[i].options[n].selected; if(option_selected){ option_li.className='open_selected'; option_li.id='selected_' + name; $('select_info_' + name).appendChild(document.createTextNode(option_li.innerHTML)); } option_li.onmouseover = function(){ this.className='open_hover';} option_li.onmouseout = function(){ if(this.id=='selected_' + name){ this.className='open_selected'; } else { this.className='open'; } } option_li.onclick = new Function("clickOptions("+i+","+n+",'"+selects[i].name+"')"); }}function mouseSelects(name){ var sincn = 'select_info_' + name; $(sincn).onmouseover = function(){ if(this.className=='tag_select') this.className='tag_select_hover'; } $(sincn).onmouseout = function(){ if(this.className=='tag_select_hover') this.className='tag_select'; } if (isIE){ $(sincn).onclick = new Function("clickSelects('"+name+"');window.event.cancelBubble = true;"); } else if(!isIE){ $(sincn).onclick = new Function("clickSelects('"+name+"');"); $('select_info_' +name).addEventListener("click", stopBubbling, false); }}function clickSelects(name){ var sincn = 'select_info_' + name; var sinul = 'options_' + name; for (i=0;i<selects.length;i++){ if(selects[i].name == name){ if( $(sincn).className =='tag_select_hover'){ $(sincn).className ='tag_select_open'; $(sinul).style.display = ''; } else if( $(sincn).className =='tag_select_open'){ $(sincn).className = 'tag_select_hover'; $(sinul).style.display = 'none'; } } else{ $('select_info_' + selects[i].name).className = 'tag_select'; $('options_' + selects[i].name).style.display = 'none'; } }}function clickOptions(i, n, name){ var li = $('options_' + name).getElementsByTagName('li'); $('selected_' + name).className='open'; $('selected_' + name).id=''; li[n].id='selected_' + name; li[n].className='open_hover'; $('select_' + name).removeChild($('select_info_' + name)); select_info = document.createElement('div'); select_info.id = 'select_info_' + name; select_info.className='tag_select'; select_info.style.cursor='pointer'; $('options_' + name).parentNode.insertBefore(select_info,$('options_' + name)); mouseSelects(name); $('select_info_' + name).appendChild(document.createTextNode(li[n].innerHTML)); $( 'options_' + name ).style.display = 'none' ; $( 'select_info_' + name ).className = 'tag_select'; selects[i].options[n].selected = 'selected';}window.onload = function(e) { bodyclick = document.getElementsByTagName('body').item(0); rSelects(); bodyclick.onclick = function(){ for (i=0;i<selects.length;i++){ $('select_info_' + selects[i].name).className = 'tag_select'; $('options_' + selects[i].name).style.display = 'none'; } }}</script></head><body><div class="center"><div id="age_sel_2"> <select name="area_selet2" id="area_select2"> <option value="请选择">请选择</option> <option value="ASP" >ASP</option> <option value="PHP" >PHP</option> <option value=".NET" >.NET</option> <option value="Csharp" >Csharp</option> <option value="Delphi" >Delphi</option> <option value="Java" >Java</option> </select></div></div></body></html>希望本文所述对大家的javascript程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
js实现可以输入的下拉列表框的方法,具体页面代码如下:js实现可输入的下拉框测试111测试222测试333
本文实例讲述了js+CSS实现模拟select控件的下拉菜单效果。分享给大家供大家参考。具体如下:这是一个JS+CSS技术实现的Select控件效果,模拟出来的
ComboBox(下拉列表框)ComboBox(下拉列表框)常用属性:valueField:基础数据值名称绑定到该下拉列表框。—提交值textField:基础数
本文实例讲述了JS+CSS实现Li列表隔行换色效果的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:CSS+Js实现Li列表隔行换色效果.myt
同事在网上的找的下拉列表框出现位置不对的和加载慢的BUG,反正多选下拉列表框实现也很简单,与其看那些结构混乱的代码,不如自己重新实现一个。先看效果:http:/