BootStrap学习1 输入提示

首先参考这个页面http://www.bootcss.com/javascript.html#typeahead

我只是把里面最基本的东西抠出来了

<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://cdnjs.bootcss.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
</body> <div class="bs-docs-example" style="background-color: #f5f5f5;">
<input type="text" class="span3" style="margin: 0 auto;" autocomplete="off" data-provide="typeahead" data-items="4" data-source="[&quot;Alabama&quot;,&quot;Alaska&quot;,&quot;Arizona&quot;,&quot;Arkansas&quot;,&quot;California&quot;,&quot;Colorado&quot;,&quot;Connecticut&quot;,&quot;Delaware&quot;,&quot;Florida&quot;,&quot;Georgia&quot;,&quot;Hawaii&quot;,&quot;Idaho&quot;,&quot;Illinois&quot;,&quot;Indiana&quot;,&quot;Iowa&quot;,&quot;Kansas&quot;,&quot;Kentucky&quot;,&quot;Louisiana&quot;,&quot;Maine&quot;,&quot;Maryland&quot;,&quot;Massachusetts&quot;,&quot;Michigan&quot;,&quot;Minnesota&quot;,&quot;Mississippi&quot;,&quot;Missouri&quot;,&quot;Montana&quot;,&quot;Nebraska&quot;,&quot;Nevada&quot;,&quot;New Hampshire&quot;,&quot;New Jersey&quot;,&quot;New Mexico&quot;,&quot;New York&quot;,&quot;North Dakota&quot;,&quot;North Carolina&quot;,&quot;Ohio&quot;,&quot;Oklahoma&quot;,&quot;Oregon&quot;,&quot;Pennsylvania&quot;,&quot;Rhode Island&quot;,&quot;South Carolina&quot;,&quot;South Dakota&quot;,&quot;Tennessee&quot;,&quot;Texas&quot;,&quot;Utah&quot;,&quot;Vermont&quot;,&quot;Virginia&quot;,&quot;Washington&quot;,&quot;West Virginia&quot;,&quot;Wisconsin&quot;,&quot;Wyoming&quot;]"> </div>
<script>
$('.typeahead').typeahead(); </script>
</html>

好了 其实这是个很没营养的例子 使用BootStrap呢 需要引用三个文件  一个是bootstrap.css

接着就是jQuery 再就是bootstrap.js

去这里下载吧http://www.bootcss.com/

或者引用这个(这下面没有包含CSS)

<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/jquery.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-transition.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-alert.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-modal.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-dropdown.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-scrollspy.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-tab.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-tooltip.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-tooltip.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-tooltip.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-collapse.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-carousel.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-typeahead.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/bootstrap-affix.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/holder/holder.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/holder/holder.js"></script>
<script src="http://twitter.github.io/bootstrap/assets/js/application.js"></script>

之后呢 就这么用

<input type="text" id="myElement"/>

        <script type="text/javascript">
/*js数组的两种定义方式
var mycars=["Saab","Volvo","BMW"];
var mycars=new Array("Saab","Volvo","BMW")
{XX:XX}是对象的一个实例
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
*/
var mySource = [{ id: 1, name: 'Terry'}, { id: 2, name: 'Mark'}, { id: 3, name: 'Jacob'}]; var jsonString = '[{"label":"System Administrator","value":"1"},{"label":"Software Tester","value":"3"},{"label":" Software Developer","value":"4"},{"label":"Senior Developer","value":"5"},{"label":"Cloud Developer","value":"6"},{"label":"Wordpress Designer","value":"7"}]'; var jsonObj = $.parseJSON(jsonString);
var sourceArr = []; for (var i = 0; i < jsonObj.length; i++) {
sourceArr.push(jsonObj[i].label);
} var sourceX=["aaa","bbb","CCC"];
$('#myElement').typeahead({
//source:mySource
source:sourceArr
}); //-----ok </script>

这里展示了两种数据源  一种是javascript对象数组  一种是json数据

其实呢 json数据也是先处理为javascript数组再用的  这里的例子是将这里json的数据处理成了sourceX那种的的形式

放在了sourceArr中

好啦 下一个随笔来写如何结合使用ajax

上一篇:Java编程思想(后)


下一篇:怎样安装CentOS 6.6之三:磁盘分区的划分和修改