先上图吧,看看效果。

JS代码:

 ; (function ($) {
var DropdownList = function (oDataSouce, oControlsContainer, oListContainer) {
this._DataSouce = typeof (oDataSouce) == "object" ? oDataSouce : $.parseJSON(oDataSouce);
this._parentElement = oControlsContainer;//容器控件对象
this._oSelectInputDiv;//选择后显示内容控件
this._ListContainer = oListContainer;//下拉列表控件对象
this._ListContainerClick = false;//判断列表是否被点击过
this._ListPostionLeft;//列表左边的距离
this._ListPostionTop;//列表上边的距离
this._IsInited = false;//是否已经初始化数据
this._MouseoutElement = false;//判断鼠标是否离开某个元素
this._RemberMouseClickNum = 1;////记录鼠标点击下拉时的次数;
this._ArrayCollection = new Array;//多选后用于存放结果; this.AllowShowCheckBox = false;//是否允许多选
this.ControlsContainerWidth = 200;//默认控件宽度为200像素
}; DropdownList.prototype = {
InitControl: function () {
var _oSelf = this;
var _oSelectBorderDiv = $("<div>", { "class": "selectBorder", style: "width:" + _oSelf.ControlsContainerWidth + "px;" }).appendTo(this._parentElement);
_oSelectBorderDiv.mouseover(function () { $(this).css({ border: "1px solid blue" }) }).mouseout(function () { $(this).css({ border: "1px solid black" }) });
_oSelf._oSelectInputDiv = $("<div>", { "class": "selectInput", style: "width:" + (_oSelf.ControlsContainerWidth - 10) + "px;" }).appendTo($(_oSelectBorderDiv));
var _oSelectImgDiv = $("<div>", { "class": "selectImg" }).appendTo($(_oSelectBorderDiv));
var _oSelectImg = $("<img>", {
src: "images/arrow.gif", style: "cursor:pointer",
click: function () {
var _oShowList = { width: _oSelectBorderDiv.width(), border: " 1px solid gray", display: "" };
if (!_oSelf._IsInited) {
_oSelf._InitData();
_oSelf._ListPostionLeft = _oSelectBorderDiv.position().left;
_oSelf._ListPostionTop = _oSelectBorderDiv.position().top + _oSelectBorderDiv.height() + 10;
_oSelf._ListContainer.css(_oShowList);
_oSelf._ListContainer.offset({ top: _oSelf._ListPostionTop, left: _oSelf._ListPostionLeft });
_oSelf._IsInited = true;
_oSelf._RemberMouseClickNum += 1;
} else {
if (_oSelf._RemberMouseClickNum % 2 == 0) {
_oSelf._MouseoutElement = true;
_oSelf._RemberMouseClickNum = 1;
} if (_oSelf._ListContainerClick) {
_oSelf._RemberMouseClickNum += 1;
_oSelf._MouseoutElement = false;
_oSelf._BindDocumentEvent();
_oSelf._ListContainer.css(_oShowList);
_oSelf._ListContainerClick = false;
}
else {
_oSelf._RemberMouseClickNum += 1;
_oSelf._BindDocumentEvent();
_oSelf._ListContainer.css(_oShowList);
}
} _oSelf._SetListboxSelectedStatus(_oSelf.AllowShowCheckBox);
}, mouseout: function () {
_oSelf._MouseoutElement = true;
_oSelf._RemberMouseClickNum += 1;
}
}).appendTo(_oSelectImgDiv);
},
_InitData: function () {
var _oSelf = this;
var _oSelectCollection;
for (var i = 0, _iDataCpount = _oSelf._DataSouce.length; i < _iDataCpount; i++) {
var _oDiv = $("<div>", {
"class": "lists", id: "div_" + i, selected: "false", selectedIndex: "" + i + "", title: "" + this._DataSouce[i].text + "", click: function () {
if (_oSelf.AllowShowCheckBox) {
_oSelf._BindListboxChecboxClickEvent(this);
} else { _oSelf._GetListboxText(this); }
}, mouseout: function () { _oSelf._MouseoutElement = true; }
}).mouseover(function () {
_oSelf._BindMouseoverEvent($(this));
_oSelf._BindDocumentEvent();
}).mouseout(function () {
_oSelf._BindMouseoutEvent($(this));
});
this._ListContainer.append(_oDiv);
if (_oSelf.AllowShowCheckBox) {
_oDiv.append($("<input>", {
type: "checkbox", id: "checkbox_" + i, "for": "label_" + i, click: function (oCheck) {
var _sID = oCheck.srcElement.id || oCheck.tagName.id;
var _oParentDiv = $("#" + _sID).parent();
_oSelf._BindListboxChecboxClickEvent(_oParentDiv);
}
}));
} _oDiv.append($("<label>", { id: "label_" + i, key: "" + this._DataSouce[i].value + "" }).html(this._DataSouce[i].text));
} if (_oSelf.AllowShowCheckBox) {
_oSelf._CreateListboxFoot();
}
},
_BindMouseoverEvent: function (oDiv) {
oDiv.mouseover(function () {
if ($(this).attr("selected") != "selected") {
$(this).css({ backgroundColor: "#3A8FCF" })
}
});
},
_BindMouseoutEvent: function (oDiv) {
oDiv.mouseout(function () {
if ($(this).attr("selected") != "selected") {
$(this).css({ backgroundColor: "#fff" })
}
});
},
_BindCheckboxClickEvent: function (oCheckBox, oDiv) {
_oSelf = this; if (!oCheckBox[0].checked) {
oCheckBox[0].checked = true; oDiv.css({ backgroundColor: "#3A8FCF" }).unbind("mouseover").unbind("mouseout");
} else {
oCheckBox[0].checked = false;
oDiv.css({ backgroundColor: "#fff" }).bind("mouseover", _oSelf._BindMouseoverEvent(oDiv)).bind("mouseout", _oSelf._BindMouseoutEvent(oDiv));
}
},
_BindListboxChecboxClickEvent: function (oDiv) {
var _oSelf = this;
var _oParentContainer = $(oDiv);
var _oCheckBoxElement = _oParentContainer.children().first();
var _oLabelElement = _oParentContainer.find("label");
_oSelf._BindCheckboxClickEvent(_oCheckBoxElement, $(oDiv));
_oSelf._MouseoutElement = false;
_oSelectCollection = { key: _oLabelElement.attr("key"), value: _oLabelElement.html(), selectedIndex: $(oDiv).attr("selectedIndex") };
_oSelf._ArrayCollection.push(_oSelectCollection);
},
_BindDocumentEvent: function () {
var _oSelf = this;
$(document).click(function () {
if (_oSelf._MouseoutElement) {
_oSelf._ListContainer.css({ display: "none" });
_oSelf._MouseoutElement = false;
_oSelf._RemberMouseClickNum = 1;
if (_oSelf.AllowShowCheckBox) {
if (_oSelf._oSelectInputDiv.html() == "") {
_oSelf._SetCheckboxSelectedStatus(false, "#fff");
_oSelf._ArrayCollection = new Array;
}
}
}
});
},
_GetListboxText: function (oDiv) {
var _oSelf = this;
var _oLabelElement = $(oDiv).find("label");
var _sSelectedText = _oLabelElement.html();
var _sSelectedValue = _oLabelElement.attr("key");
var _oDisplayText = _oSelf._oSelectInputDiv;
var _iSelectedIndex = $(oDiv).attr("selectedIndex");
_oDisplayText.html(_sSelectedText);
_oDisplayText.attr({ "key": _sSelectedValue, "selected": true, "selectedIndex": _iSelectedIndex, title: "" + _sSelectedText + "" });
_oSelf._SetListboxDisplayStatus();
},
_SetListboxDisplayStatus: function () {
var _oSelf = this;
_oSelf._ListContainer.css({ display: "none" });
_oSelf._ListContainerClick = true;
_oSelf._MouseoutElement = false;
},
_SetListboxSelectedStatus: function (bAllowShowCheckBox) {
var _oSelf = this;
if (bAllowShowCheckBox) {
if (_oSelf._ArrayCollection.length > 0) {
_oSelf._SetCheckboxSelectedStatus(true, "#3A8FCF")
}
} else {
var _sCurrentSelectedText = _oSelf._oSelectInputDiv.attr("selectedIndex");
for (var libIndex = 0, libLen = _oSelf._ListContainer.children().length; libIndex < libLen; libIndex++) {
var _oDiv = _oSelf._ListContainer.children().eq(libIndex);
if (_oDiv.attr("selectedIndex") == _sCurrentSelectedText) {
_oDiv.attr("selected", true).css({ background: "#3A8FCF" });
} else { _oDiv.attr("selected", false).css({ background: "#fff" }); }
}
}
},
_SetCheckboxSelectedStatus: function (bChecked, sColor) {
var _oSelf = this;
for (var _iAcIndex = 0, _iAcLen = _oSelf._ArrayCollection.length; _iAcIndex < _iAcLen; _iAcIndex++) {
var _oDiv = _oSelf._ListContainer.children().eq(_oSelf._ArrayCollection[_iAcIndex].selectedIndex);
_oDiv.find("input[type=checkbox]")[0].checked = bChecked;
_oDiv.css({ background: sColor });
}
}, _CreateListboxFoot: function () {
var _oSelf = this;
$("<div>", { "class": "foot", selectedIndex: "9999" }).append(
$("<img>", {
src: "images/ok.gif", click: function () {
var _sRsult = "";
for (var _iAcIndex = 0, _iAcLen = _oSelf._ArrayCollection.length; _iAcIndex < _iAcLen; _iAcIndex++) {
if (_iAcIndex == 0) {
_sRsult += _oSelf._ArrayCollection[_iAcIndex].value + ";";
}
if (_iAcIndex == _iAcLen - 1) {
_sRsult += _oSelf._ArrayCollection[_iAcIndex].value;
}
}
_oSelf._oSelectInputDiv.html(_sRsult);
_oSelf._MouseoutElement = false;
_oSelf._ListContainer.css({ display: "none" });
}
})).append($("<img>", {
src: "images/cancle.gif", click: function () {
_oSelf._SetCheckboxSelectedStatus(false, "#fff");
_oSelf._ArrayCollection = new Array();
_oSelf._MouseoutElement = true;
_oSelf._BindDocumentEvent();
_oSelf._oSelectInputDiv.html("");
}
})).appendTo(_oSelf._ListContainer);
},
};
$.extend(true, window, {
Controls: { DropdownList: DropdownList }
})
}(jQuery));

下面是HTML

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/JsDropdownList.css" rel="stylesheet" />
<script src="Js/jquery-1.8.3.min.js"></script>
<script src="Js/JsDropdownList.js"></script>
<script type="text/javascript">
$(function () {
var _oDropdownList = new Controls.DropdownList([{ "value": , "text": "测试测试测试测试测试测试1" }, { "value": , "text": "测试测试测试测试测试测试2" }, { "value": , "text": "测试3" }, { "value": , "text": "测试4" }], $("#div_Container"), $("#div_list"));
_oDropdownList.AllowShowCheckBox = true;
_oDropdownList.InitControl();
});
</script>
</head>
<body>
<table>
<tr>
<td>选择:</td>
<td>
<div id="div_Container">
</div>
</td>
</tr>
<tr>
<td>选择:</td>
<td>
<div id="div1" style="float: left;">
<input id="Text2" type="text" />
</div>
</td>
</tr>
<tr>
<td>选择:</td>
<td>
<div id="div2" style="float: left;">
<input id="Text1" type="text" />
</div>
</td>
</tr>
</table>
<div id="div_list" style="display: none; overflow: hidden; padding: 0px; margin: 0px;">
</div>
</body>
</html>

还是那句话,表喷啊。 希望各位给点宝贵的意见, 第一次弄这个东西,还不太会。 见谅!!

  后续打算写一个grid表格控件,并结合这个下拉框控件和后台处理程序。grid还在构思中。不知道咋个插入代码下载连接,需要代码的童靴可以留个邮箱吧, 或者直接拷贝上面的代码。

纯手工打造dropdownlist控件的更多相关文章

  1. 纯手工打造漂亮的瀑布流,五大插件一个都不少Bootstrap+jQuery+Masonry+imagesLoaded+Lightbox!

    前两天写的文章<纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!>受到很多网友的喜爱,今天特别推出姊妹篇<纯手工打造漂亮的瀑 ...

  2. [置顶] 纯手工打造漂亮的瀑布流,五大插件一个都不少Bootstrap+jQuery+Masonry+imagesLoaded+Lightbox!

    前两天写的文章<纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!>受到很多网友的喜爱,今天特别推出姊妹篇<纯手工打造漂亮的瀑 ...

  3. DropDownList控件

    1.DropDownList控件 <asp:DropDownList runat="server" ID="DropDownList1" AutoPost ...

  4. DropDownList 控件不能触发SelectedIndexChanged 事件

    相信DropDownList 控件不能触发SelectedIndexChanged 事件已经不是什么新鲜事情了,原因也无外乎以下几种: 1.DropDownList 控件的属性 AutoPostBac ...

  5. 三级联动---DropDownList控件

    AutoPostBack属性:意思是自动回传,也就是说此控件值更改后是否和服务器进行交互比如Dropdownlist控件,若设置为True,则你更换下拉列表值时会刷新页面(如果是网页的话),设置为fl ...

  6. c#中DropDownList控件绑定枚举数据

    c# asp.net 中DropDownList控件绑定枚举数据 1.枚举(enum)代码: private enum heros { 德玛 = , 皇子 = , 大头 = , 剑圣 = , } 如果 ...

  7. DropDownList 控件

    今天打算学习下dropdownlist控件的取值,当你通过数据库控件或dataset绑定值后,但又希望显示指定的值,这可不是简单的值绑定就OK,上网搜了一些资料,想彻底了解哈,后面发现其中有这么大的奥 ...

  8. DropDownList控件学习

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. 客户端用JavaScript填充DropDownList控件 服务器端读不到值

    填充没有任何问题,但是在服务器端却取不出来下拉表中的内容.页面代码如下. <form id="form1" runat="server"> < ...

随机推荐

  1. Duff in Love - 588B(素数的判断)

    题目大意:如果一个数的所有因子都不是别的数的平方,那么这个数就是lovely数,现在给定一个数,求出这个数所有因子里面最大的lovely数 分析:很有意思的一道题,如果把这个数因子分解成最基本的苏因子 ...

  2. 用CS-Script把Notepad++变身支持智能提示和运行代码的C#集成开发环境

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用CS-Script把Notepad++变身支持智能提示和运行代码的C#集成开发环境.

  3. C# 动态创建出来的窗体间的通讯 delegate1

    附件 http://files.cnblogs.com/xe2011/CSharp_WindowsForms_delegate01.rar 需要每个窗体是独立存在,禁止相与引用窗体 这样干净并且可以反 ...

  4. cocos2dx 以子弹飞行为例解说拖尾效果类CCMotionStreak

    在游戏开发中,有时会须要在某个游戏对象上的运动轨迹上实现渐隐效果.比方子弹的运动轨迹,假设不借助引擎的帮助,这样的效果则须要通过大量的图片来实现.而Cocos2D-x的拖动渐隐效果类CCMotionS ...

  5. The content of element type "beans" must match "(description?,(import|alias|bean)*)

    The content of element type "beans" must match "(description?,(import|alias|bean)*) - ...

  6. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

  7. Freemarker学习中遇到的问题

    在网上找到了尚学堂的视频,同时有书和源码等资料.但是在跟着练习的过程中,代码运行报了错: 2015-7-20 22:26:40 freemarker.log.JDK14LoggerFactory$JD ...

  8. flexbox 兼容安卓4.3

                 border:1px solid red;              overflow: hidden;                           font-siz ...

  9. iOS UIKit:CollectionView之布局(2)

    Collection view使用UICollectionViewFlowLayout对象来管理section中的cell,该对象是一种流布局方式,即在collection view中的section ...

  10. AES加解密【示例】

    代码 /**  * AES算法加密.JRE默认只能用16个字节(128)位密钥  */ public class AESUtils {     //使用指定转换的 Cipher 对象     publ ...