当前位置: 首页 > 图文教程 > 网络编程 > Javascript > js 自定义的联动下拉框

Javascript
为调试JavaScript添加输出窗口的代码
Js 中debug方式
一些mootools的学习资源
JavaScript 精粹读书笔记(1,2)
CutePsWheel javascript libary 控制输入文本框为可使用滚轮控制的js库
数组Array进行原型prototype扩展后带来的for in遍历问题
javascript 鼠标拖动图标技术
比较搞笑的js陷阱题
js 自定义的联动下拉框
js 省地市级联选择
JavaScript 类似flash效果的立体图片浏览器
JavaScript Event学习第九章 鼠标事件
jQuery AJAX回调函数this指向问题
toString()一个会自动调用的方法
jQuery 文本框模拟下拉列表效果
关于页面被拦截的问题
javascript 解析url的search方法
一个XML格式数据转换为图表的例子
Javascript 获取链接(url)参数的方法[正则与截取字符串]
一些收集整理非常不错的JS效果代码

Javascript 中的 js 自定义的联动下拉框


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 77 ::
收藏到网摘: n/a

一直都嫌下拉框这个html控件难看,之前弄了个<div><ul><li></li></ul></div>版的下拉框.

觉得这个下拉框已经稍微能满足美观需求了,

这个是点出来的效果,写了键盘的方向键,回车,esc等 事件, 并且能根据页面的底部距离判断是否向上展示

今天弄了个联动的,顺便贴部分代码

效果预览:

以下代码解决了ie6的兼容问题

复制代码 代码如下:

$containerDivText.mousedown(function() {
setTimeout(
function() {
if ($newUl[0].style.display == 'block') {
$newUl.hide();
positionHideFix();
return false;
}
$containerDiv.focus();
//show list
$newUl.slideDown(100);
positionFix();
//when keys are pressed
document.onkeydown = function(e) {
if (e == null) { // ie
var keycode = event.keyCode;
} else { // everything else
var keycode = e.which;
}
//enter key or esc key pressed, hide list
if (keycode == 13 || keycode == 27) {
$newUl.hide();
positionHideFix();
return false;
}
}
}, 1);
//the function settimeout is used for ie6, because if you click the element where you hava focused on the element,
//ie6 would think you click it twice(2010-2-4)
});

以下代码解决了下拉框事件定义功能匮乏问题
代码
复制代码 代码如下:

if (!opts.callbackfn) {
$newLi.click(function(e) {
var $clickedLi = jQuery(e.target),
text = $clickedLi.text();
//update counter
currentIndex = $newLi.index($clickedLi);
//remove all hilites, then add hilite to selected item
$newLi.removeClass('hiLite');
$clickedLi.addClass('hiLite');
setSelectText(text);
$newUl.hide();
$containerDiv.css('position', 'static'); //ie
});
} else {
$newLi.click(function(e) {
var $clickedLi = jQuery(e.target),
text = $clickedLi.text();
//update counter
currentIndex = $newLi.index($clickedLi);
//remove all hilites, then add hilite to selected item
$newLi.removeClass('hiLite');
$clickedLi.addClass('hiLite');
setSelectText(text);
$newUl.hide();
$containerDiv.css('position', 'static'); //ie
(opts.callbackfn)(this.value);
});
} //param callbackfn means you can define a event function from every li in the ul;(2010-2-4)

然后是页面的应用,
代码
复制代码 代码如下:

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#my-dropdown2").hide();
jQuery('#my-dropdown1').sSelect(
{ defaultText: "",
callbackfn: function(value) {
if (value == 1) {
jQuery("#my-dropdown2_list").parent().remove();
jQuery("#linkc_value").val(value);
return;
}
jQuery.getJSON(
'/Department.mvc/GetSubDepartment?DepartmentID=' + value,
function(list) {
jQuery("#my-dropdown2_list").parent().remove();
jQuery("#my-dropdown2").html("");
var temp = "";
temp += "<option value=''>请选择部门</option>";
for (var i = 0; i < list.length; i++) {
temp += "<option value=" + list[i].DepartmentID + ">" + list[i].DepartmentName + "</option>";
}
jQuery("#my-dropdown2").html(temp);
jQuery("#my-dropdown2").show();
jQuery('#my-dropdown2').sSelect({
callbackfn: function(value) {
jQuery("#linkc_value").val(value);
}
});
}
);
}
}
);
// killErrors = function(){ return true; }
// window.onerror = killErrors;
});
function linkc() {
location.href = "/User.mvc/Front?DepartmentID=" + jQuery("#linkc_value").val();
}
</script>

这三块只是部分
但是花的时间较多 其它代码不贴了 有问题留言吧。。。