JS CSS drop-down menu background problem_html/css_WEB-ITnose
Jun 24, 2016 pm 12:22 PM
select scroll bar
The following code changes the style of the drop-down menu and can be used normally. However, there is a problem now that there is a lot of content in the drop-down menu. I think there will be a scroll bar automatically at a certain height. Could anyone help me please, thank you very much.
<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 太平洋時(shí)間(美國和加拿大)</option> <option value="GMT-7:00">GMT-7:00 山地時(shí)間(美國和加拿大)</option> <option value="GMT-6:00">GMT-6:00 中部時(shí)間(美國和加拿大)</option> <option value="GMT-5:00">GMT-5:00 東部時(shí)間(美國和加拿大)</option> <option value="GMT-4:30">GMT-4:30 加拉斯加</option> <option value="GMT-4:00">GMT-4:00 大西洋時(shí)間(加拿大)</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 巴格達(dá)、科威特、莫斯科</option> <option value="GMT+3:30">GMT+3:30 德黑蘭</option> <option value="GMT+4:00">GMT+4:00 高加索標(biāo)準(zhǔn)時(shí)間</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 阿拉木圖、新西伯利亞、達(dá)卡</option> <option value="GMT+6:30">GMT+6:30 仰光</option> <option value="GMT+7:00">GMT+7:00 曼谷、河內(nèi)、雅加達(dá)</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 阿德萊德、達(dá)爾文</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>
Reply to discussion (solution)
The default overflow is visible. It will open automatically. Just give #macstyle1 ul.tag_options a suitable height and set overflow-y:auto;. For the sake of beauty, overflow-x can be set to hidden. After the scroll bar comes out, the width is a bit insufficient. It is recommended to add a little more.
<!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 太平洋時(shí)間(美國和加拿大)</option> <option value="GMT-7:00">GMT-7:00 山地時(shí)間(美國和加拿大)</option> <option value="GMT-6:00">GMT-6:00 中部時(shí)間(美國和加拿大)</option> <option value="GMT-5:00">GMT-5:00 東部時(shí)間(美國和加拿大)</option> <option value="GMT-4:30">GMT-4:30 加拉斯加</option> <option value="GMT-4:00">GMT-4:00 大西洋時(shí)間(加拿大)</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 巴格達(dá)、科威特、莫斯科</option> <option value="GMT+3:30">GMT+3:30 德黑蘭</option> <option value="GMT+4:00">GMT+4:00 高加索標(biāo)準(zhǔn)時(shí)間</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 阿拉木圖、新西伯利亞、達(dá)卡</option> <option value="GMT+6:30">GMT+6:30 仰光</option> <option value="GMT+7:00">GMT+7:00 曼谷、河內(nèi)、雅加達(dá)</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 阿德萊德、達(dá)爾文</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>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

The rational use of semantic tags in HTML can improve page structure clarity, accessibility and SEO effects. 1. Used for independent content blocks, such as blog posts or comments, it must be self-contained; 2. Used for classification related content, usually including titles, and is suitable for different modules of the page; 3. Used for auxiliary information related to the main content but not core, such as sidebar recommendations or author profiles. In actual development, labels should be combined and other, avoid excessive nesting, keep the structure simple, and verify the rationality of the structure through developer tools.

To use HTML button elements to achieve clickable buttons, you must first master its basic usage and common precautions. 1. Create buttons with tags and define behaviors through type attributes (such as button, submit, reset), which is submitted by default; 2. Add interactive functions through JavaScript, which can be written inline or bind event listeners through ID to improve maintenance; 3. Use CSS to customize styles, including background color, border, rounded corners and hover/active status effects to enhance user experience; 4. Pay attention to common problems: make sure that the disabled attribute is not enabled, JS events are correctly bound, layout occlusion, and use the help of developer tools to troubleshoot exceptions. Master this

Metadata in HTMLhead is crucial for SEO, social sharing, and browser behavior. 1. Set the page title and description, use and keep it concise and unique; 2. Add OpenGraph and Twitter card information to optimize social sharing effects, pay attention to the image size and use debugging tools to test; 3. Define the character set and viewport settings to ensure multi-language support is adapted to the mobile terminal; 4. Optional tags such as author copyright, robots control and canonical prevent duplicate content should also be configured reasonably.

TolearnHTMLin2025,chooseatutorialthatbalanceshands-onpracticewithmodernstandardsandintegratesCSSandJavaScriptbasics.1.Prioritizehands-onlearningwithstep-by-stepprojectslikebuildingapersonalprofileorbloglayout.2.EnsureitcoversmodernHTMLelementssuchas,

Using HTML sums allows for intuitive and semantic clarity to add caption text to images or media. 1. Used to wrap independent media content, such as pictures, videos or code blocks; 2. It is placed as its explanatory text, and can be located above or below the media; 3. They not only improve the clarity of the page structure, but also enhance accessibility and SEO effect; 4. When using it, you should pay attention to avoid abuse, and apply to content that needs to be emphasized and accompanied by description, rather than ordinary decorative pictures; 5. The alt attribute that cannot be ignored, which is different from figcaption; 6. The figcaption is flexible and can be placed at the top or bottom of the figure as needed. Using these two tags correctly helps to build semantic and easy to understand web content.

How to make HTML mail templates with good compatibility? First, you need to build a structure with tables to avoid using div flex or grid layout; secondly, all styles must be inlined and cannot rely on external CSS; then the picture should be added with alt description and use a public URL, and the buttons should be simulated with a table or td with background color; finally, you must test and adjust the details on multiple clients.

class, id, style, data-, and title are the most commonly used global attributes in HTML. class is used to specify one or more class names to facilitate style setting and JavaScript operations; id provides unique identifiers for elements, suitable for anchor jumps and JavaScript control; style allows for inline styles to be added, suitable for temporary debugging but not recommended for large-scale use; data-properties are used to store custom data, which is convenient for front-end and back-end interaction; title is used to add mouseover prompts, but its style and behavior are limited by the browser. Reasonable selection of these attributes can improve development efficiency and user experience.

When there is no backend server, HTML form submission can still be processed through front-end technology or third-party services. Specific methods include: 1. Use JavaScript to intercept form submissions to achieve input verification and user feedback, but the data will not be persisted; 2. Use third-party serverless form services such as Formspree to collect data and provide email notification and redirection functions; 3. Use localStorage to store temporary client data, which is suitable for saving user preferences or managing single-page application status, but is not suitable for long-term storage of sensitive information.
