メモ@inudaisho

君見ずや出版 / 興味次第の調べ物置き場

ちょっとまえにつくった関数 プルダウンつーのかな

function pullDown( oThis,arrText ){ 
	if ( event.keyCode == '40' ){//↓
		if(! oThis.childDiv ){
			oThis.childDiv=document.createElement('div');
			document.forms[0].appendChild(oThis.childDiv);
			oThis.childDiv.add =function(arrOption){
				var lenOp = arrOption.length;
				for( index = 0 ;index <lenOp;index++){
					var oChDiv = document.createElement( 'div' );
					this.appendChild(oChDiv);
					oChDiv.innerText=arrOption[index];
					oChDiv.style.width=oThis.offsetWidth - 2 + 'px';
					oChDiv.style.backgroundColor='#fff';
					oChDiv.style.fontSize='10pt';
					oChDiv.onclick=function(){
						oThis.value=this.innerText;
						this.parentNode.parentNode.removeChild(this.parentNode);
						oThis.childDiv = null;
					}
					oChDiv.onmouseover=function(){
						this.style.backgroundColor='#009';
						this.style.color='#fff';
					}
					oChDiv.onmouseout=function(){
						this.style.backgroundColor='#fff';
						this.style.color='#000';
					}
				}
			}
			if( 0 < oThis.value.length){
				arrText = reduceArr(arrText,oThis.value);
			};
			arrCum = cumulativeOffset(oThis);
			oThis.childDiv.add(arrText);
			oThis.childDiv.style.border = '1 solid #002';
			oThis.childDiv.style.backgroundColor= '#fff';
			oThis.childDiv.style.position = 'absolute';
			oThis.childDiv.style.top = arrCum[1] + oThis.offsetHeight + 'px';
			oThis.childDiv.style.left = arrCum[0] + 'px';
		}
	}else if(event.keyCode=='38'){//↑
		if(oThis.childDiv){
			oThis.childDiv.parentNode.removeChild(oThis.childDiv);
			oThis.childDiv=null;
		}
	}
}
function cumulativeOffset(obj){ //prototype.js
	var valueT = 0, valueL = 0;
	do {
		valueT += obj.offsetTop  || 0;
		valueL += obj.offsetLeft || 0;
		obj = obj.offsetParent;
	} while (obj);
	return [valueL, valueT];
}
function reduceArr(arrTarget,strTarget){
	tempArr=[];
	lenArr = arrTarget.length;
	lenStr = strTarget.length;
	for(index=0;index<lenArr;index++){
		valeach = arrTarget[index];
		if(valeach.substr(0,lenStr) == strTarget){
			tempArr.push(valeach);
		}
	}
	return tempArr;
}