📄 combobox.htc
字号:
/*******************************************************************************/
Copyright (c) 2002, Jeremy Bettis <jeremy@deadbeef.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*******************************************************************************/<PUBLIC:COMPONENT tagName="COMBOBOX"><PUBLIC:property name="textfield" /><PUBLIC:property name="popup" /><PUBLIC:property name="multiple" /><PUBLIC:property name="value" get="getvalue" put="putvalue" id="prop_value" /><PUBLIC:property name="selectedIndex" get="getselectedIndex" put="putselectedIndex" id="prop_selectedIndex" /><PUBLIC:property name="options" get="getoptions" id="prop_options" /><PUBLIC:attach event="ondocumentready" handler="setUpComboBox" /><PUBLIC:event name="onkeypress" id="onkeypressevent" /><PUBLIC:event name="onchange" id="onchangeevent" /><SCRIPT language="JScript"> function setUpComboBox() { if (null == popup) { element.innerHTML="<select>" + element.innerHTML + "</select>"; popup = element.children(0); element.appendChild(popup); var prop; for (prop in element) { var val = element.getAttribute(prop,3); if (val != null && val != "" && prop != "name" && prop != "size" && prop != "text" && prop != "Index") { popup.setAttribute(prop,val); } } popup.id = element.uniqueID; popup.name = element.name; popup.selectedIndex = element.Index; popup.attachEvent("onchange", takeValueFromPopup); popup.attachEvent("onfocus", popupfocus); popup.attachEvent("onblur", popupblur); } if (null == textfield) { textfield = document.createElement("INPUT"); textfield.id = element.uniqueID; textfield.name = element.name; if(element.disabled == true) textfield.disabled = true; textfield.value = element.text; textfield.associatedPopup = popup; textfield.accessKey=element.accessKey; element.accessKey=""; popup.textfield = textfield; if (element.size != null && element.size > 0 && popup.currentStyle.width == "auto") { textfield.size=element.size; } else if (element.currentStyle.width != null && element.currentStyle.width.indexOf("%") != -1 && element.clientWidth > 0){ var pct=(element.clientWidth-16)/element.clientWidth*100-1; if (pct>0 && pct<=100) textfield.style.width=pct+'%'; } else { textfield.style.posWidth=element.clientWidth-16; } textfield.attachEvent("onchange",takeValueFromTextfield); textfield.attachEvent("onkeyup",completeTyping); textfield.attachEvent("onkeydown",txtkeydown); textfield.attachEvent("onkeypress", txtkeypress); //element.removeAttribute("NAME",0); //element.removeAttribute("SIZE",0); element.parentElement.insertBefore(textfield, element); textfield.style.marginRight='16px'; if(parseInt(element.Index) > -1) takeValueFromPopupAt(popup); } popup.style.position="absolute"; popup.tabIndex=-1; popup.style.setExpression("posWidth", "textfield.clientWidth+20", "JScript"); popup.style.left = '1px'; popup.findLeftOffset=findLeftOffset; popup.style.setExpression("posLeft", "findLeftOffset(textfield)", "JScript"); popup.style.top = '1px'; popup.findTopOffset=findTopOffset; popup.style.setExpression("posTop", "findTopOffset(textfield)", "JScript"); popup.style.setExpression("posHeight", "textfield.clientHeight", "JScript"); popup.style.setExpression("clip","'rect(auto auto auto '+(textfield.clientWidth+2)+')'", "JScript"); if(parseInt(element.Index) > -1) popup.needsblur=false; else popup.needsblur=true; //sizestatus.setExpression("value",popup.id+".style.posLeft + ' ' + "+popup.id+".style.posTop + ' ' + "+popup.id+".style.posWidth+ ' ' + "+popup.id+".style.posHeight + 'topMargin' + body.topMargin", "JScript"); } function takeValueFromPopup() { takeValueFromPopupAt(event.srcElement); } function takeValueFromPopupAt(popup) { var idx=popup.selectedIndex; var textfield=popup.textfield; if (idx != null && idx >= 0) { textfield.value = popup.options[idx].text; } if (popup.needsblur) { textfield.focus(); textfield.select(); } var evt = createEventObject(); evt.srcElement = element; onchangeevent.fire(); } function takeValueFromTextfield() { var txt = event.srcElement; var popup = txt.associatedPopup; var idx; for(idx=0;idx<popup.options.length;idx++) { if (popup.options[idx].text == txt.value) { popup.selectedIndex=idx; return; } } popup.selectedIndex=-1; var evt = createEventObject(); evt.srcElement = element; onchangeevent.fire(); } function popupblur() { var popup = event.srcElement; popup.needsblur=false; } function popupfocus() { var popup = event.srcElement; popup.needsblur=true; } function txtkeydown() { // Down Arrow if (event.keyCode == 40) { if (popup.selectedIndex < popup.options.length-1) popup.selectedIndex += 1; takeValueFromPopupAt(popup); } // Up Arrow if (event.keyCode == 38) { if (popup.selectedIndex > 0) popup.selectedIndex -= 1; else if (popup.selectedIndex == -1) popup.selectedIndex = popup.options.length-1; takeValueFromPopupAt(popup); } //window.status=popup.selectedIndex+" of "+popup.options.length; } function txtkeypress() { var realevent = window.event; var evt = createEventObject(); evt.altKey = realevent.altKey; evt.altLeft = realevent.altLeft; evt.cancelBubble = realevent.cancelBubble; evt.returnValue = realevent.returnValue; evt.ctrlKey = realevent.ctrlKey; evt.ctrlLeft = realevent.ctrlLeft; evt.keyCode = realevent.keyCode; evt.repeat = realevent.repeat; evt.shiftKey = realevent.shiftKey; evt.shiftLeft = realevent.shiftLeft; evt.type = realevent.type; evt.srcElement = element; onkeypressevent.fire(evt); realevent.cancelBubble = evt.cancelBubble; realevent.returnValue = evt.returnValue; realevent.keyCode = evt.keyCode; } function completeTyping() { //window.status="key = "+event.keyCode; if (event.keyCode < 0x2f && event.keyCode != 32) return; var text = event.srcElement.value; var popup = event.srcElement.associatedPopup; var options = popup.options; var i; var utext = text.toUpperCase(); for(i=0;i<options.length;i++) { var newtxt = options[i].text; var uopt = newtxt.toUpperCase(); if (uopt != utext && 0 == uopt.indexOf(utext)) { var txtrange = event.srcElement.createTextRange(); event.srcElement.value = text + newtxt.substr(text.length); //txtrange.collapse(true); txtrange.moveStart("character", text.length); txtrange.select(); takeValueFromTextfield(); break; } } } function findLeftOffset(elem) { var left = 0; while (elem) { left += elem.offsetLeft; elem = elem.offsetParent; } return left; } function findTopOffset(elem) { var top = 0; while (elem) { top += elem.offsetTop; elem = elem.offsetParent; } return top; } function getvalue() { if (popup.selectedIndex >= 0) { return popup.value; } return textfield.value; } function putvalue(val) { var idx; for(idx=0;idx<popup.options.length;idx++) { if (popup.options[idx].value == val) { popup.selectedIndex=idx; prop_selectedIndex.fireChange(); textfield.value = popup.options[idx].text; prop_value.fireChange(); return; } } textfield.value = val; prop_value.fireChange(); for(idx=0;idx<popup.options.length;idx++) { if (popup.options[idx].text == val) { popup.selectedIndex=idx; prop_selectedIndex.fireChange(); return; } } popup.selectedIndex=-1; prop_selectedIndex.fireChange(); } function getselectedIndex() { return popup.selectedIndex; } function putselectedIndex(idx) { popup.selectedIndex=idx; takeValueFromPopupAt(popup); prop_selectedIndex.fireChange(); prop_value.fireChange(); } function getoptions() { return popup.options; }</SCRIPT></PUBLIC:COMPONENT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -