展会信息港展会大全

用jQuery模拟select下拉框的简单示例代码
来源:互联网   发布日期:2016-03-25 15:51:19   浏览:1920次  

导读:本篇文章主要是对用jQuery模拟select下拉框的简单示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助很多时候,美工会觉得默认的select下拉框很难看(特别是右侧的下拉箭...

本篇文章主要是对用jQuery模拟select下拉框的简单示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助

很多时候,美工会觉得默认的select下拉框很难看(特别是右侧的下拉箭头按钮),他们通常喜欢用一个自定义的图标来代替这个按钮。这样就只能用 js + div 来模拟了,倒腾了一番,用jQuery模拟了下,当然网上这种文章也不少,只是懒得去看找

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>自己实现的下拉框</title>

<style type="text/css" media="all">

*{font-size:12px;line-height:18px;list-style:none;padding:0;margin:0;text-decoration:none;color:#000;border:0}

.page{text-align:center;margin:50px;}

input{border-bottom:solid 1px #ccc;height:18px}

.expand{display:none;position:absolute;width:200px;height:100px;overflow-y:auto;border:solid 1px #ccc};

.expand li{margin:1px 0;background:#fff}

.expand a{text-decoration:none;display:block;padding:0 5px;background:#efefef;margin:1px 0}

.expand a:hover{background:#ff9}

</style>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.min.js"></script>

<script type="text/javascript">

function showExpand(targetId, expandId, expand_class) {

//先关掉其它弹出的层

if (expand_class != undefined) {

$("." + expand_class).hide();

}

//判断是否为IE

var isIE = (! +[1, ]);

var expand = $("#" + expandId);

var target = $("#" + targetId);

var dx = 0;

if (isIE) {

dx = -2;

}

else {

dx = 0;

}

expand.get(0).style.left = target.get(0).getBoundingClientRect().left + dx + "px";

if (isIE) {

dx = 17;

}

else {

dx = 19;

}

expand.get(0).style.top = parseInt(target.get(0).getBoundingClientRect().top) + dx + "px";

expand.show();

//每个li点击时赋值

$("#" + expandId).find("li").each(function (i) {

$(this).show().click(function () {

target.val($(this).text().split(' ')[1]);

expand.hide();

})

})

}

function search(srcId, expandId) {

var expand = $("#" + expandId);

var src = $("#" + srcId);

var A = expand.find("a");

var v = src.val().toUpperCase();

A.each(function (i) {

if (v.length >= 2) {

if ($(this).text().toUpperCase().indexOf(v) == -1) {

$(this).parent().hide();

}

else {

$(this).parent().show();

}

}

if (v.length <= 0) {

$(this).parent().show();

}

})

src.val(v);

}

$().ready(function(){

$("#txt_city").keyup(function(){

search('txt_city','city_select1');

}).focus(function(){

showExpand('txt_city','city_select1','expand')

})

$("#txt_city2").keyup(function(){

search('txt_city2','city_select2');

}).focus(function(){

showExpand('txt_city2','city_select2','expand')

})

})

function fnTest(){

document.getElementById("txtTarget").value = document.getElementById("txtSrc").value;

}

</script>

</head>

<body>

<div class="page" style="text-align: center">

<input type="text" value="" id="txt_city" class="input_expand"/><a

href="#" onclick="showExpand('txt_city','city_select1','expand')">

赞助本站

人工智能实验室

相关热词: javascript js

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港