模仿select,类似select的下拉框。

工作需要把页面中的所有select标签替换为其他类似的下拉框,模仿了好久,终于成功。感到非常欣慰,特写出来分享一下。有更好建议的可以提出来,把这个小东东改的更好一些。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="js中 regedit_name,regedit都是为了方便点击其他地方,收缩下拉框的">
<META NAME="Description" CONTENT="模拟select,使用说明:1:id = ‘ComboBox’ 必须要保留,这样可以当点击其他地方时收缩列表,但是可以修改;2: id='DropDown_1',否则当点击该节点下拉框会隐藏">
</HEAD>
<BODY>
<div>
<TABLE id = "ComboBox2" cellSpacing=1 cellPadding=0 border=0 bgColor="black"
style="display:inline;" >
<TR bgColor = '#FFFFFF'>
<TD style="width:9">
<input type='text' style="height:18;border:1 ;width:80px" ></input>
</TD>
<TD>
<TABLE id = "DropDown" cellSpacing=0 cellPadding = 0 border = 0 bgColor="#DBD8D1"
style="display:inline;cursor:default;"
onmousedown = "fnMousedown()"
>
<TR>
<TD style="font-family: webdings;font-size:8px;text-align: center;width:13px;height:18px;" id="DropDown_1">6</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<TABLE cellSpacing=1 cellPadding=0 border=1 bgColor="#FFFFFF"
style="display:'';border-color:black;border-collapse: collapse;border-style:solid;font-size:9pt;cursor:default; solid #666666;"
></TABLE>
</div>
<SCRIPT LANGUAGE="JavaScript">
window.document.attachEvent("onclick", chkstat);
var regedit_name = ["ComboBox2"];
var regedit = {};
for(var i=0;i<regedit_name.length;i++)
{
regedit[regedit_name[i]] = document.getElementById(regedit_name[i]);
}
function chkstat()
{
var oEl = event.srcElement;
var el_id = oEl.id;
//alert();
if(el_id)//针对有id 的对象
{
var ComboBox_el = regedit[el_id];
if(ComboBox_el)
{
var ComboBox_nextSibling = ComboBox_el.nextSibling;
if(ComboBox_nextSibling.style.display != "none")
{
ComboBox_nextSibling.style.display = "none";
}
}
}
else
{
for(var i=0;i<regedit_name.length;i++)
{
var combox_1 = document.getElementById(regedit_name[i]);
if(!combox_1) return;
var combox_sibling = combox_1.nextSibling;

if(combox_sibling)if(combox_sibling.style.display != "none")
{
combox_sibling.style.display = "none";
}
}
}
}
function DropListClick()
{
obj = event.srcElement;
var DropList_1 = obj.parentNode.parentNode.parentNode;
var ComboBoxIpt = DropList_1.previousSibling.firstChild.firstChild.firstChild.firstChild
ComboBoxIpt.value = obj.innerText;
DropList_1.style.display = "none";
ComboBoxIpt.select();
DropDown.style.color = '#000000';
}
function fnMousedown()
{
var aNodes=[1,2,3,4,5,6];
var srcelement = event.srcElement;
if(!srcelement){ return;}
var ComboBox = srcelement.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
var DropList = ComboBox.nextSibling;
if(DropList)
{
f_PublicCreateDropListData(DropList,aNodes);//向DropList里添加数值
DropList.style.width = (DropList.offsetWidth < ComboBox.offsetWidth) ? ComboBox.offsetWidth : DropList.offsetWidth;
DropList.style.left = getx(ComboBox);
DropList.style.top = gety(ComboBox)+19;
DropList.style.display = '';
}
}

function f_PublicCreateDropListData(DropList,aNodes,redata)//向DropList中插入td ,option
{
if(!redata)
var re=new RegExp("^");
else
var re=new RegExp("^"+redata,"i");

var otd = null;
var No=DropList.rows.length;
if(No)for(i=0;i<No;i++)DropList.deleteRow();
for(var i=0;i<aNodes.length;i++)
{
if(re.test(aNodes[i])==true)
{
otd = DropList.insertRow().insertCell();
otd.style.height = "12px";//设定td高度
otd.style.borderColor = "black";
otd.innerHTML = aNodes[i];//设定td内的数值
otd.attachEvent("onmouseover",DropListOver);//td绑定事件
otd.attachEvent("onmouseout",DropListOut);
otd.attachEvent("onclick",DropListClick);
}
}

}
function DropListOver()
{
obj = event.srcElement;
obj.bgColor='highlight';
obj.style.color='#FFFFFF';
obj.style.cursor='default';
}
function DropListOut()
{
obj = event.srcElement;
obj.bgColor='#FFFFFF';
obj.style.color='#000000';
}

function getx(e)
{
var l=e.offsetLeft;
while(e=e.offsetParent){
l+=e.offsetLeft;
}
return l;
}
function gety(e)
{
var t=e.offsetTop;
while(e=e.offsetParent){
t+=e.offsetTop;
}
return t;
}
</SCRIPT>
</BODY>
</HTML>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值