vue基础案例4 - 即时搜索

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>




    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <style>
        [v-cloak]{
            display:none;
        }

        *{
            margin: 0;
            padding: 0;
        }

        body{
            font: 15px/1.3 'Open Sans',sans-serif;
            color: #5e5b64;
            text-align: center;
        }

        a,a:visited{
            outline: none;
            color: #389dc1;
        }
        a:hover{
            text-decoration: none;
        }

        section,footer,header,aside,nav{
            display: block;
        }

        .bar{
            background: #5c9bb7;

            background-image:-webkit-linear-gradient(top,#5c9bb7,#5392ad);
            background-image:-moz-linear-gradient(top,#5c9bb7,#5392ad);
            background-image:linear-gradient(top,#5c9bb7,#5392ad);

            box-shadow: 0 1px 1px #ccc;
            border-radius: 2px;
            width: 400px;
            padding: 14px;
            margin: 45px auto 20px;
            position: relative;
        }

        .bar input{
            background: #fff no-repeat 13px 13px;
            /*background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyBpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8w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Zrad437sCGHOfUtcziutuYu2v8XUFF/4f6vMK/YgAH1HxkBYV60AR31gxkBYd6xAeF3VzMCwvzOBpypX8V4yuFRzX2d2gD/l5yjH4fYQEnzkj4fae5rJulF2sMXVrAsaTWttRFu4Osb+1jEDT71/ZveyhouTch2fINQL9hKefKjuYFfuznXWzXMTabyrvfyIV3M4vhXgAEAUMs7K0J9UJAAAAAASUVORK5CYII=);*/

            border: none;
            width: 100%;
            line-height: 19px;
            padding: 11px 0;

            border-radius: 2px;
            box-shadow: 0 2px 8px #c4c4c4 inset;
            text-align: left;
            font-size: 14px;
            font-family: inherit;
            color: #738289;
            font-weight: bold;
            outline: none;
            text-indent: 40px;
        }

        ul{
            list-style: none;
            width: 428px;
            margin: 0 auto;
            text-align: left;
        }

        ul li{
            border-bottom: 1px solid #ddd;
            padding: 10px;
            overflow: hidden;
            border: none;
        }

        ul li img{
            width: 60px;
            height: auto;
            float: left;
        }

        ul li p{
            margin-left: 75px;
            font-weight: bold;
            padding-top: 12px;
            color: #6e7a7f;
        }
    </style>

  </head>
  <body>

    <form id="main" v-cloak>
        <div class="bar">
            <input type="text" v-model="searchString" placeholder="请输入你的搜索词..." />

        </div>

        <ul>
            <!-- searchString为函数参数 -->
            <li v-for="i in articles | searchFor searchString">     
                <a v-bind:href="i.url">
                    <img v-bind:src="i.image" />
                    <p>{{i.title}}</p>
                </a>
            </li>
        </ul>

    </form>





   <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.16/vue.js"></script>

   <script>
    // 自定义过滤器,注意该处的value指的是数组articles
    Vue.filter('searchFor',function(value,searchString){
        // console.log(value);
        // console.log(searchString);

        var result = [];

        if(!searchString){
            return value;
        }

        searchString = searchString.trim().toLowerCase();

        // filter()方法创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素
        result = value.filter(function(item){
            // 如果搜索词被包含,则返回匹配的内容
            if(item.title.toLowerCase().indexOf(searchString) !== -1){
                return item;
            }
        });

        return result;
    });

     var demo = new Vue({
         el:"#main",
         data:{
             searchString : "",
             articles:[
                 {
                    "title" : "What You Need To Know About CSS Variables",
                    "url" : "www.baidu.com/",
                    "image" : "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513702065552&di=31cc963325776654684e2a05a7e2c61c&imgtype=0&src=http%3A%2F%2Fimg.taopic.com%2Fuploads%2Fallimg%2F120727%2F201995-120HG1030762.jpg"
                 },
                 {
                    "title" : "Freebie: 4 Great Looking Pricing Tables",
                    "url" : "www.google.com/",
                    "image" : "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513702065552&di=06b72962d3319bdb0ca14d0ded4f51ac&imgtype=0&src=http%3A%2F%2Fimg07.tooopen.com%2Fimages%2F20170316%2Ftooopen_sy_201956178977.jpg"
                 },
                 {
                    "title" : "20 Interesting JavaScript and CSS Libraries for February 2016",
                    "url" : "www.sougou.com/",
                    "image" : "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513702065552&di=d2fb75d53377e893090d620c7989b136&imgtype=0&src=http%3A%2F%2Fdl.bizhi.sogou.com%2Fimages%2F2012%2F01%2F19%2F174522.jpg"
                 }
             ],
         }
     });



   </script>

  </body>
</html>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值