当前页面: 开发资料首页 → Javascript 专题 → 模仿combox(select)控件
模仿combox(select)控件
摘要 : 模仿combox(select)控件
<tr>
<td>
不用整天为美化select控件烦恼了。
运行代码框
<textarea class=fm cols=78 id=code rows=12> HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<head>
simulate combox control - http://www.never-online.net
</head>
<body>
<script LANGUAGE="JavaScript">
//-------------------------------------------------------------
// @ Module: simulate select control, IE only.
// @ Debug in: IE 6.0
// @ Script by: blueDestiny, never-online
// @ Updated: 2006-3-22
// @ Version: 1.0 apha
// @ Email: blueDestiny [at] 126.com
// @ Website: http://www.never-online.net
// @ Please Hold this item please.
//
// API
// @ class: simulateSelect()
//
// @ object.style(ctlStyle[,selStyle][,unselStyle])
// ctlStyle: main control combox css class name
// selStyle: when mouseover or option focus css class name
// unselStyle: options blur's css class name
//
// @ object.width=(widthPX)
// widthPX must be a digit number.
//
// @ object.height=(heightPX)
// heightPX must be a digit number.
//
// @ object.getValue(ctlSelID)
// ctlSelID is the unique select control ID
//
// -------------- for the next Version ----------
// @ object.readOnly = (blnReadOnly)
// blnReadOnly must be a boolean type or a number type.
// @ object.addEvent(w, h)
// w: fire handler's event.
// h: handler function.
//-------------------------------------------------------------
function $(objID)
{
return document.getElementById(objID);
};
function Offset(e)
{
var t = e.offsetTop;
var l = e.offsetLeft;
var w = e.offsetWidth;
var h = e.offsetHeight-2;
while(e=e.offsetParent)
{
t+=e.offsetTop;
l+=e.offsetLeft;
}
return {
top : t,
left : l,
width : w,
height : h
}
}
//-----------------------------------------------
function simulateSelect() { with(this)
{
this.IDs = [];
this.name = this;
// property for beta Version
// can editable combox
this.readonly = true;
this.height = 20;
this.width = null;
this.ctlStyle = "CtlSelect";
this.selStyle = "selected";
this.unselStyle = "unselected";
this.elementPrefix = "e__";
this.inputPrefix = "i__";
this.containerPrefix = "c__";
this.buttonPrefix = "b__";
return this;
}};
simulateSelect.prototype.init = function(ctlSelIDs) { with(this)
{
eval(name).append(ctlSelIDs);
eval(name).simulates();
}};
simulateSelect.prototype.style = function() { with(this)
{
ctlStyle = arguments[0];
selStyle = arguments[1];
unselStyle = arguments[2];
}};
//-----------------------------------------------
simulateSelect.prototype.append = function(ctlSelIDs) { with(this)
{
if( ctlSelIDs.indexOf(",")>0 )
{
var arrCtlSel = ctlSelIDs.split(",");
for(var i=0; i
-1 ||
el.id.indexOf(inputPrefix)>-1 ||
el.id.indexOf(containerPrefix)>-1 ||
el.id.indexOf(buttonPrefix)>-1 )
{
return;
}
else
{
for(var i=0; i0 ? width : object.offsetWidth);
height ? input.style.height=button.style.height=height : "";
input.value = object[0].text;
if( readonly==true ) input.readOnly=true;
// this method is only IE.
object.insertAdjacentElement("afterEnd",button);
object.insertAdjacentElement("afterEnd",input);
object.style.display = 'none';
}};
simulateSelect.prototype.expand = function(ctlSelID, offset) { with(this)
{
var div, btn_off;
var object = $(ctlSelID);
if( !$(containerPrefix + ctlSelID) )
{
div = document.createElement("DIV");
div.style.position = "absolute";
div.style.display = "block";
div.setAttribute("id", containerPrefix + ctlSelID);
div.className = ctlStyle;
div.style.left = offset.left;
div.style.top = offset.top + offset.height;
div.style.width = (width ? width : offset.width) + 20;
div.style.height = offset.height;
document.body.appendChild(div);
for(var i=0; i simulate combox control
demonstrate
<select id="s0">
<option value="- please select your options -"> - please select your options -</option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
<option value="4">option4</option>
<option value="5">option5</option>
</select>
<select id="s1">
<option value="- please select your options -"> - please select your options -</option>
<option value="1">1option1</option>
<option value="2">1option2</option>
<option value="3">1option3</option>
<option value="4">1option4</option>
<option value="5">1option5</option>
</select>
<select id="s2">
<option value="- please select your options -"> - please select your options -</option>
<option value="1">2option1</option>
<option value="2">2option2</option>
<option value="3">2option3</option>
<option value="4">2option4</option>
<option value="5">2option5</option>
</select>
<select id="s3">
<option value="- please select your options -"> - please select your options -</option>
<option value="1">3option1</option>
<option value="2">3option2</option>
<option value="3">3option3</option>
<option value="4">3option4</option>
<option value="5">3option5</option>
</select>
Get value
<script LANGUAGE="JavaScript">
var a = new simulateSelect();
a.style("CtlSelect", "selected", "unselected");
a.init("s1");
var b = new simulateSelect();
b.style("CtlSelect1", "selected1", "unselected1");
b.width = 300;
b.init("s2");
var c = new simulateSelect();
c.style("CtlSelect2", "selected2", "unselected2");
c.width = 320;
c.init("s3");
//
</script>
description
//-------------------------------------------------------------
// @ Module: simulate select control, IE only.
// @ Debug in: IE 6.0
// @ Script by: blueDestiny, never-online
// @ Updated: 2006-3-22
// @ Version: 1.0 apha
// @ Email: blueDestiny [at] 126.com
// @ Website: http://www.never-online.net
// @ Please Hold this item please.
//
// API
// @ simulateSelect(ctlSelIDs)
// ctlSelIDs: select control IDs, split by ","
//
// @ simulateSelect.style(ctlStyle[,selStyle][,unselStyle])
// ctlStyle: main control combox css class name
// selStyle: when mouseover or option focus css class name
// unselStyle: options blur's css class name
//
// @ simulateSelect.width=(widthPX)
// widthPX must be a digit number.
//
// @ simulateSelect.height=(heightPX)
// heightPX must be a digit number.
//
// -------------- for the next Version ----------
// @ simulateSelect.readOnly = (blnReadOnly)
// blnReadOnly must be a boolean type or a number type.
// @ simulateSelect.addEvent(w, h)
// w: fire handler's event.
// h: handler function.
//-------------------------------------------------------------
Power By blueDestiny, never-online
http://www.never-online.net
</body>
</textarea>
<input onclick="runEx('code')" onfocus=this.blur() style="CURSOR: hand" type=button value=运行代码> <input name=Submit onclick=cycode(code) onfocus=this.blur() style="CURSOR: hand; WIDTH: 95px" type=button value=复制到剪贴板> <input onclick=svcode(code) onfocus=this.blur() style="CURSOR: hand" type=button value=另存代码> [Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
</td>
</tr>
</table>
↑返回目录
前一篇: 常用JavaScript网页广告代码
后一篇: COOL而实用的动态效果