📄 niceforms.js
字号:
var selectWidth = parseInt(selects[q].className.replace(/width_/g, ""));
center.style.width = selectWidth - 10 + 'px';
selectArea.style.width = selectWidth + selectRightSideWidth + selectLeftSideWidth + 'px';
button.style.width = selectWidth + selectRightSideWidth + selectLeftSideWidth + 'px';
button.style.marginLeft = - selectWidth - selectLeftSideWidth + 'px';
button.href = "javascript:showOptions("+q+")";
button.onkeydown = selectEvent;
button.className = "selectButton"; //class used to check for mouseover
selectArea.className = "selectArea";
selectArea.id = "sarea"+q;
left.className = "left";
right.className = "right";
center.className = "center";
right.appendChild(button);
center.appendChild(text);
selectArea.appendChild(left);
selectArea.appendChild(right);
selectArea.appendChild(center);
//hide the select field
selects[q].style.display='none';
//insert select div
selects[q].parentNode.insertBefore(selectArea, selects[q]);
//build & place options div
var optionsDiv = document.createElement('div');
optionsDiv.style.width = selectWidth + 1 + 'px';
optionsDiv.className = "optionsDivInvisible";
optionsDiv.id = "optionsDiv"+q;
optionsDiv.style.left = findPosX(selectArea) + 'px';
optionsDiv.style.top = findPosY(selectArea) + selectAreaHeight - selectAreaOptionsOverlap + 'px';
//get select's options and add to options div
for(var w = 0; w < selects[q].options.length; w++) {
var optionHolder = document.createElement('p');
var optionLink = document.createElement('a');
var optionTxt = document.createTextNode(selects[q].options[w].text);
optionLink.href = "javascript:showOptions("+q+"); selectMe('"+selects[q].id+"',"+w+","+q+");";
optionLink.appendChild(optionTxt);
optionHolder.appendChild(optionLink);
optionsDiv.appendChild(optionHolder);
//check for pre-selected items
if(selects[q].options[w].selected) {selectMe(selects[q].id,w,q);}
}
//insert options div
document.getElementsByTagName("body")[0].appendChild(optionsDiv);
}
}
function showOptions(g) {
elem = document.getElementById("optionsDiv"+g);
if(elem.className=="optionsDivInvisible") {elem.className = "optionsDivVisible";}
else if(elem.className=="optionsDivVisible") {elem.className = "optionsDivInvisible";}
elem.onmouseout = hideOptions;
}
function hideOptions(e) { //hiding the options on mouseout
if (!e) var e = window.event;
var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
if(((reltg.nodeName != 'A') && (reltg.nodeName != 'DIV')) || ((reltg.nodeName == 'A') && (reltg.className=="selectButton") && (reltg.nodeName != 'DIV'))) {this.className = "optionsDivInvisible";};
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
function selectMe(selectFieldId,linkNo,selectNo) {
//feed selected option to the actual select field
selectField = document.getElementById(selectFieldId);
for(var k = 0; k < selectField.options.length; k++) {
if(k==linkNo) {selectField.options[k].selected = "selected";}
else {selectField.options[k].selected = "";}
}
//show selected option
textVar = document.getElementById("mySelectText"+selectNo);
var newText = document.createTextNode(selectField.options[linkNo].text);
textVar.replaceChild(newText, textVar.childNodes[0]);
}
function selectEvent(e) {
if (!e) var e = window.event;
var thecode = e.keyCode;
switch(thecode){
case 40: //down
var fieldId = this.parentNode.parentNode.id.replace(/sarea/g, "");
var linkNo = 0;
for(var q = 0; q < selects[fieldId].options.length; q++) {if(selects[fieldId].options[q].selected) {linkNo = q;}}
++linkNo;
if(linkNo >= selects[fieldId].options.length) {linkNo = 0;}
selectMe(selects[fieldId].id, linkNo, fieldId);
break;
case 38: //up
var fieldId = this.parentNode.parentNode.id.replace(/sarea/g, "");
var linkNo = 0;
for(var q = 0; q < selects[fieldId].options.length; q++) {if(selects[fieldId].options[q].selected) {linkNo = q;}}
--linkNo;
if(linkNo < 0) {linkNo = selects[fieldId].options.length - 1;}
selectMe(selects[fieldId].id, linkNo, fieldId);
break;
default:
break;
}
}
function replaceTexts() {
for(var q = 0; q < texts.length; q++) {
texts[q].style.width = texts[q].size * 10 + 'px';
txtLeft = document.createElement('img'); txtLeft.src = imagesPath + "input_left.gif"; txtLeft.className = "inputCorner";
txtRight = document.createElement('img'); txtRight.src = imagesPath + "input_right.gif"; txtRight.className = "inputCorner";
texts[q].parentNode.insertBefore(txtLeft, texts[q]);
texts[q].parentNode.insertBefore(txtRight, texts[q].nextSibling);
texts[q].className = "textinput";
//create hovers
texts[q].onfocus = function() {
this.className = "textinputHovered";
this.previousSibling.src = imagesPath + "input_left_xon.gif";
this.nextSibling.src = imagesPath + "input_right_xon.gif";
}
texts[q].onblur = function() {
this.className = "textinput";
this.previousSibling.src = imagesPath + "input_left.gif";
this.nextSibling.src = imagesPath + "input_right.gif";
}
}
}
function replaceTextareas() {
for(var q = 0; q < textareas.length; q++) {
var where = textareas[q].parentNode;
var where2 = textareas[q].previousSibling;
textareas[q].style.width = textareas[q].cols * 10 + 'px';
textareas[q].style.height = textareas[q].rows * 10 + 'px';
//create divs
var container = document.createElement('div');
container.className = "txtarea";
container.style.width = textareas[q].cols * 10 + 20 + 'px';
container.style.height = textareas[q].rows * 10 + 20 + 'px';
var topRight = document.createElement('div');
topRight.className = "tr";
var topLeft = document.createElement('img');
topLeft.className = "txt_corner";
topLeft.src = imagesPath + "txtarea_tl.gif";
var centerRight = document.createElement('div');
centerRight.className = "cntr";
var centerLeft = document.createElement('div');
centerLeft.className = "cntr_l";
if(!this.ie) {centerLeft.style.height = textareas[q].rows * 10 + 10 + 'px';}
else {centerLeft.style.height = textareas[q].rows * 10 + 12 + 'px';}
var bottomRight = document.createElement('div');
bottomRight.className = "br";
var bottomLeft = document.createElement('img');
bottomLeft.className = "txt_corner";
bottomLeft.src = imagesPath + "txtarea_bl.gif";
//assemble divs
container.appendChild(topRight);
topRight.appendChild(topLeft);
container.appendChild(centerRight);
centerRight.appendChild(centerLeft);
centerRight.appendChild(textareas[q]);
container.appendChild(bottomRight);
bottomRight.appendChild(bottomLeft);
//insert structure
where.insertBefore(container, where2);
//create hovers
textareas[q].onfocus = function() {
this.previousSibling.className = "cntr_l_xon";
this.parentNode.className = "cntr_xon";
this.parentNode.previousSibling.className = "tr_xon";
this.parentNode.previousSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_tl_xon.gif";
this.parentNode.nextSibling.className = "br_xon";
this.parentNode.nextSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_bl_xon.gif";
}
textareas[q].onblur = function() {
this.previousSibling.className = "cntr_l";
this.parentNode.className = "cntr";
this.parentNode.previousSibling.className = "tr";
this.parentNode.previousSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_tl.gif";
this.parentNode.nextSibling.className = "br";
this.parentNode.nextSibling.getElementsByTagName("img")[0].src = imagesPath + "txtarea_bl.gif";
}
}
}
function buttonHovers() {
for (var i = 0; i < buttons.length; i++) {
buttons[i].className = "buttonSubmit";
var buttonLeft = document.createElement('img');
buttonLeft.src = imagesPath + "button_left.gif";
buttonLeft.className = "buttonImg";
buttons[i].parentNode.insertBefore(buttonLeft, buttons[i]);
var buttonRight = document.createElement('img');
buttonRight.src = imagesPath + "button_right.gif";
buttonRight.className = "buttonImg";
if(buttons[i].nextSibling) {buttons[i].parentNode.insertBefore(buttonRight, buttons[i].nextSibling);}
else {buttons[i].parentNode.appendChild(buttonRight);}
buttons[i].onmouseover = function() {
this.className += "Hovered";
this.previousSibling.src = imagesPath + "button_left_xon.gif";
this.nextSibling.src = imagesPath + "button_right_xon.gif";
}
buttons[i].onmouseout = function() {
this.className = this.className.replace(/Hovered/g, "");
this.previousSibling.src = imagesPath + "button_left.gif";
this.nextSibling.src = imagesPath + "button_right.gif";
}
}
}
//Useful functions
function findPosY(obj) {
var posTop = 0;
while (obj.offsetParent) {posTop += obj.offsetTop; obj = obj.offsetParent;}
return posTop;
}
function findPosX(obj) {
var posLeft = 0;
while (obj.offsetParent) {posLeft += obj.offsetLeft; obj = obj.offsetParent;}
return posLeft;
}
window.onload = init;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -