jQuery插件插件datalist实现很好看的实现很好看的input下拉列表下拉列表
本文给大家分享的是使用jQuery实现的房HTML5中的一个好看的input框很好看的下拉列表--datalist,兼容性非
常不错,这里推荐给大家,有需要的小伙伴可以参考下。
HTML5中定义了一种input框很好看的下拉列表--datalist,然而目前它的支持性并不好(万恶的IE,好在你要渐渐退役了...)。于
是最近更据需求写了一个小型datalist插件,兼容到IE8(IE7应该没多少人用了吧?)。实现的具体需求如下:
当被选中的时候(触发blur焦点)(不管是鼠标还是tab键)清空input框并且显示自定义的下拉列表,然后可以用键盘的上下键选
择(鼠标当然肯定没理由不可以啦),单击鼠标左键或者enter键将选中的列表的值输入到input框。
具体的实现代码如下:
HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index, follow" />
<meta name="googlebot" content="index, follow" />
<meta name="author" content="codetker" />
<title> 表单选中弹出框</title>
<link href="css/reset.css" type="text/css" rel="Stylesheet" />
<link href="css/master.css" type="text/css" rel="Stylesheet" />
<script type="text/javascript" src="js/jquery-1.11.0.js"></script>
</head>
<body>
<div class="wrap">
<form class="center">
<div class="input_wrap">
<input name="input1" class="input input1" type="text"/>
<ul class="input1_ul select_list">
<li>问题1</li>
<li>问题2</li>
<li>问题3</li>
<li>问题4</li>
<li>问题5</li>
</ul>
</div>
</form>
</div>
<script type="text/javascript" src="js/jquery.codetker.datalist.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".input_wrap").myDatalist({"bgcolor":"red","widths":1,"heights":1});
});
</script>
</body>
</html>
CSS(reset.css里面是初始化浏览器默认值用的,这里是style.css)
.wrap{ margin:0 auto; font-family: "微软雅黑";font-size: 14px;}
.center{ margin: 0 auto; width:500px;}
.input{ margin: 0; padding: 0; /*border:none;*/ width:140px; height: 24px; float:left;}
.select_list{display: none; position:absolute; z-index: 100;}
.select_list li{ height:24px; margin: 0; padding: 0; background-color: #fff; cursor: pointer; list-style: none; position:relative;}
.select_list li:hover{ background-color: red;}
.input_wrap{ position:relative; }
JavaScript
/*
datalist 0.1
自定义datalist插件,实现html5中input元素datalist的效果
兼容IE8+,Firefox,Chrome等常见浏览器
*/
;(function($,window,document,undefined){ //undefinde是真实的undefined,并非参数
//将可选择的变量传递给方法
评论0