select 滚动条
下面的代码是改变下拉菜单样式的,可以正常使用,但是现在有个问题就是下拉的内容很多,我想到一定高度就自动有滚动条,麻烦哪位兄弟帮我看看,感觉感谢。
<script language="JavaScript" type="text/javascript">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>
<style type="text/css">body{margin:0px; padding:0px; font-size:12px; color:#FFFFFB; background:#3d4045; }div,ul,li{margin:0; padding:0;}#macstyle1 .select_box{height:22px; width:271px;}#macstyle1 div.tag_select{display:block;color:#fff; text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_hover{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_open{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 ul.tag_options{position:absolute;list-style:none;text-align:left; }#macstyle1 ul.tag_options li{ background:#414348;text-align:left; padding-left:10px;width:241px;height:15px;line-height:15px;color:#fff;overflow:auto;}#macstyle1 ul.tag_options li.open_hover{color:#fff; background:#6c6f75; }#macstyle1 ul.tag_options li.open_selected{color:#fff; }</style>
<div id="macstyle1"> <select > <option value="GMT-12:00">GMT-12:00 日界线西</option> <option value="GMT-11:00">GMT-11:00 中途岛,萨摩亚群岛</option> <option value="GMT-10:00">GMT-10:00 夏威夷</option> <option value="GMT-9:00">GMT-9:00 阿拉斯加</option> <option value="GMT-8:00">GMT-8:00 太平洋时间(美国和加拿大)</option> <option value="GMT-7:00">GMT-7:00 山地时间(美国和加拿大)</option> <option value="GMT-6:00">GMT-6:00 中部时间(美国和加拿大)</option> <option value="GMT-5:00">GMT-5:00 东部时间(美国和加拿大)</option> <option value="GMT-4:30">GMT-4:30 加拉斯加</option> <option value="GMT-4:00">GMT-4:00 大西洋时间(加拿大)</option> <option value="GMT-3:30">GMT-3:30 纽芬兰</option> <option value="GMT-3:00">GMT-3:00 乔治郭、巴西利亚</option> <option value="GMT-2:00">GMT-2:00 中大西洋</option> <option value="GMT-1:00">GMT-1:00 佛得角群岛、亚速尔群岛</option> <option value="GMT-0:00">GMT-0:00 都柏林、爱丁堡、伦敦</option> <option value="GMT+1:00">GMT+1:00 阿姆斯特丹、柏林、罗马、巴黎</option> <option value="GMT+2:00">GMT+2:00 雅典、耶路撒冷、伊斯坦布尔</option> <option value="GMT+3:00">GMT+3:00 巴格达、科威特、莫斯科</option> <option value="GMT+3:30">GMT+3:30 德黑兰</option> <option value="GMT+4:00">GMT+4:00 高加索标准时间</option> <option value="GMT+4:30">GMT+4:30 喀布尔</option> <option value="GMT+5:00">GMT+5:00 伊斯兰堡、卡拉奇、塔什干</option> <option value="GMT+5:30">GMT+5:30 马德拉斯、孟买、新德里</option> <option value="GMT+5:45">GMT+5:45 加德满都</option> <option value="GMT+6:00">GMT+6:00 阿拉木图、新西伯利亚、达卡</option> <option value="GMT+6:30">GMT+6:30 仰光</option> <option value="GMT+7:00">GMT+7:00 曼谷、河内、雅加达</option> <option value="GMT+8:00" selected="selected">GMT+8:00 北京、乌鲁木齐、新加坡</option> <option value="GMT+9:00">GMT+9:00 汉城、东京、大阪、札幌</option> <option value="GMT+9:30" >GMT+9:30 阿德莱德、达尔文</option> <option value="GMT+10:00" >GMT+10:00 墨尔本、悉尼、堪培拉</option> <option value="GMT+11:00">GMT+11:00 马加丹、索罗门群岛</option> <option value="GMT+12:00">GMT+12:00 奥克兰、惠灵顿</option> <option value="GMT+13:00">GMT+13:00 努库阿洛法</option> </select> </div>
回复讨论(解决方案)
默认overflow是visible,会自动撑开,给#macstyle1 ul.tag_options一个合适的高度并让overflow-y:auto;就行了,为了美观overflow-x可以设置为hidden。出来滚动条之后宽度有点不够,建议加一点。
<!doctype html><html><head><meta charset="utf-8" /><style type="text/css">body{margin:0px; padding:0px; font-size:12px; color:#FFFFFB; background:#3d4045; }div,ul,li{margin:0; padding:0;}#macstyle1 .select_box{height:22px; width:271px;}#macstyle1 div.tag_select{display:block;color:#fff; text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_hover{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 div.tag_select_open{display:block;color:#fff;text-align:left; padding-left:10px;width:271px;height:22px;background:url(http://img.bbs.csdn.net/upload/201307/03/1372819568_91197.gif) no-repeat;line-height:22px;}#macstyle1 ul.tag_options{position:absolute;list-style:none;text-align:left;height:300px;overflow-y:auto;overflow-x:hidden;}#macstyle1 ul.tag_options li{ background:#414348;text-align:left; padding-left:10px;width:241px;height:15px;line-height:15px;color:#fff;overflow:auto;}#macstyle1 ul.tag_options li.open_hover{color:#fff; background:#6c6f75; }#macstyle1 ul.tag_options li.open_selected{color:#fff; }</style><script language="JavaScript" type="text/javascript">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 id="macstyle1"> <select > <option value="GMT-12:00">GMT-12:00 日界线西</option> <option value="GMT-11:00">GMT-11:00 中途岛,萨摩亚群岛</option> <option value="GMT-10:00">GMT-10:00 夏威夷</option> <option value="GMT-9:00">GMT-9:00 阿拉斯加</option> <option value="GMT-8:00">GMT-8:00 太平洋时间(美国和加拿大)</option> <option value="GMT-7:00">GMT-7:00 山地时间(美国和加拿大)</option> <option value="GMT-6:00">GMT-6:00 中部时间(美国和加拿大)</option> <option value="GMT-5:00">GMT-5:00 东部时间(美国和加拿大)</option> <option value="GMT-4:30">GMT-4:30 加拉斯加</option> <option value="GMT-4:00">GMT-4:00 大西洋时间(加拿大)</option> <option value="GMT-3:30">GMT-3:30 纽芬兰</option> <option value="GMT-3:00">GMT-3:00 乔治郭、巴西利亚</option> <option value="GMT-2:00">GMT-2:00 中大西洋</option> <option value="GMT-1:00">GMT-1:00 佛得角群岛、亚速尔群岛</option> <option value="GMT-0:00">GMT-0:00 都柏林、爱丁堡、伦敦</option> <option value="GMT+1:00">GMT+1:00 阿姆斯特丹、柏林、罗马、巴黎</option> <option value="GMT+2:00">GMT+2:00 雅典、耶路撒冷、伊斯坦布尔</option> <option value="GMT+3:00">GMT+3:00 巴格达、科威特、莫斯科</option> <option value="GMT+3:30">GMT+3:30 德黑兰</option> <option value="GMT+4:00">GMT+4:00 高加索标准时间</option> <option value="GMT+4:30">GMT+4:30 喀布尔</option> <option value="GMT+5:00">GMT+5:00 伊斯兰堡、卡拉奇、塔什干</option> <option value="GMT+5:30">GMT+5:30 马德拉斯、孟买、新德里</option> <option value="GMT+5:45">GMT+5:45 加德满都</option> <option value="GMT+6:00">GMT+6:00 阿拉木图、新西伯利亚、达卡</option> <option value="GMT+6:30">GMT+6:30 仰光</option> <option value="GMT+7:00">GMT+7:00 曼谷、河内、雅加达</option> <option value="GMT+8:00" selected="selected">GMT+8:00 北京、乌鲁木齐、新加坡</option> <option value="GMT+9:00">GMT+9:00 汉城、东京、大阪、札幌</option> <option value="GMT+9:30" >GMT+9:30 阿德莱德、达尔文</option> <option value="GMT+10:00" >GMT+10:00 墨尔本、悉尼、堪培拉</option> <option value="GMT+11:00">GMT+11:00 马加丹、索罗门群岛</option> <option value="GMT+12:00">GMT+12:00 奥克兰、惠灵顿</option> <option value="GMT+13:00">GMT+13:00 努库阿洛法</option> </select></div> </body></html>

HTML是一种用于构建网页的语言,通过标签和属性定义网页结构和内容。1)HTML通过标签组织文档结构,如、。2)浏览器解析HTML构建DOM并渲染网页。3)HTML5的新特性如、、增强了多媒体功能。4)常见错误包括标签未闭合和属性值未加引号。5)优化建议包括使用语义化标签和减少文件大小。

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

HTML的作用是通过标签和属性定义网页的结构和内容。1.HTML通过到、等标签组织内容,使其易于阅读和理解。2.使用语义化标签如、等增强可访问性和SEO。3.优化HTML代码可以提高网页加载速度和用户体验。

htmlisaspecifictypefodyfocusedonstructuringwebcontent,而“代码” badlyLyCludEslanguagesLikeLikejavascriptandPytyPythonForFunctionality.1)htmldefineswebpagertuctureduseTags.2)“代码”代码“ code” code code code codeSpassSesseseseseseseseAwiderRangeLangeLangeforLageforLogageforLogicIctInterract

HTML、CSS和JavaScript是Web开发的三大支柱。1.HTML定义网页结构,使用标签如、等。2.CSS控制网页样式,使用选择器和属性如color、font-size等。3.JavaScript实现动态效果和交互,通过事件监听和DOM操作。

HTML定义网页结构,CSS负责样式和布局,JavaScript赋予动态交互。三者在网页开发中各司其职,共同构建丰富多彩的网站。

HTML适合初学者学习,因为它简单易学且能快速看到成果。1)HTML的学习曲线平缓,易于上手。2)只需掌握基本标签即可开始创建网页。3)灵活性高,可与CSS和JavaScript结合使用。4)丰富的学习资源和现代工具支持学习过程。

AnexampleOfAstartingTaginHtmlis,beginSaparagraph.startingTagSareEssentialInhtmlastheyInitiateEllements,defiteTheeTheErtypes,andarecrucialforsstructuringwebpages wepages webpages andConstructingthedom。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver Mac版
视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具