Categoryjs article

Vue

Hallo Vue

<body>
    <div id="app">{{message}}</div>
</body>
<script>
    //let(定义变量)/const(定义常量)
    const app = new Vue({
        el: '#app',    //用于挂载要管理的元素
        data: {        //定义数据
            message: 'Hello Vue!'
        }
    })
</script>

vue的列表展示(v-for)

<body>
    <div id="app">
        <ul>
            <li v-for="item in movies">
                {{item}}
            </li>
        </ul>
    </div>
</body>
<script>
    const app = new Vue({
        el: '#app',
        data: {
            movies: ['星际穿越', '盗梦空间', '大话西游']
        }
    })
    app.movies.push('闻香识女人');
</script>

实现:

  • 星际穿越
  • 盗梦空间
  • 大话西游
  • 闻香识女人

计数器(v-on监听,"v-on:"可用@代替称为语法糖)

  1. <body>
        <div id="app">
            <h2>当前计数:{{counter}}</h2>
            <button @click="counter++">+</button>
            <button @click="counter--">-</button>
        </div>
    </body>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                counter: 0
            }
        })
    </script>
  2. <body>
        <div id="app">
            <h2>当前计数:{{counter}}</h2>
            <!--<button v-on:click="counter++">+</button>
            <button v-on:click="counter--">-</button>-->
            <button @click="add">+</button>
            <button @click="sub">-</button>
        </div>
    </body>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                counter: 0
            },
            methods: {
                add: function(){
                    this.conter++
                },
                sub: function(){
                    this.conter--
                }
            }
        })
    </script>

02-插值操作

插值操作-mustache语法({{massage}})

<body>
    <h2>{{message}}</h2>
    <h2>{{message}},ABC</h2>
    <!--在mustache语法中,不仅仅可以直接写变量,也可以写简单的表达式-->
    <h2>{{firstname + lastname}}</h2>
    <h2>{{firstname + ' ' + lastname}}</h2>
    <h2>{{firstname}} {{lastname}}</h2>
    <h2>{{counter * 2}}</h2>
</body>
<script>
    //let(定义变量)/const(定义常量)
    const app = new Vue({
        el: '#app',    //用于挂载要管理的元素
        data: {        //定义数据
            message: 'Hello Vue!',
            firstname: 'kobe',
            lastname: 'bryant',
            counter: 100
        }
    })
</script>

v-once——只执行一次

  1. 该指令后面不需要跟任何表达式
  2. 该指令表示元素和组件只渲染一次,不会随着数据的改变而改变
<body>
    <div id="app">
        <h2 v-once>{{massage}}</h2>
        <!--massage改变时,界面并没有重新渲染-->
    </div>
</body>

v-html——解析html指令

  1. 该指令后面往往会跟上一个string类型
  2. 会将string的html解析出来并且进行渲染
<body>
    <div id="app">
        <h2 v-html="url"></h2>
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                url: '<a href="http://www.baidu.com">百度一下</a>'
            }
        })
    </script>
</body>

v-text——与mustache相似

  1. 接受一个string类型
  2. 覆盖当前文本
<body>
    <div id="app">
        <h2 v-text="message"></h2>
        <!--等于<h2>{{message}}</h2>-->
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                message: 'Azhang'
            }
        })
    </script>
</body>

v-pre——用于跳过这个元素和它子元素的编译过程,用于显示原本的mustache语法

<body>
    <div id="app">
        <h2 v-pre>{{message}}</h2>
        <!--直接显示'{{message}}'-->
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                message: 'Azhang'
            }
        })
    </script>
</body>

v-cloak

<style>
    [v-cloak]{
        display: none;
    }
    //给带有v-cloak属性的元素附上该css属性
</style>
<body>
    <div id="app" v-cloak>
        {{message}}
        <!--1s后显示'Azhang'-->
    </div>
    
    <script>
        //在vue解析之前,div中有一个属性叫v-cloak
        //在vue解析之后,div中没有一个属性叫v-cloak
        steTimeout(function(){
            const app = new vue({
                el: '#app',
                data: {
                    message: 'Azhang'
                }
            })
        }, 1000)
    </script>
</body>

03-动态绑定属性

v-bind——动态绑定属性

<body>
    <div id="app">
        <img v-bind:src="imgURL" alt="">
        <a v-bind:href="aHref">百度一下</a>
        <!--语法糖“:” <img :src="imgURL" alt="">
        <a :href="aHref">百度一下</a>-->
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                imgURL: '图片地址',
                aHref: 'http://www.baidu.com'
            }
        })
    </script>
</body>

v-bind——动态绑定class(对象语法)

<style>
    .active{
        color: red;
    }
</style>
<body>
    <div id="app">
        用法1:
        <!--<h2 class="active">{{message}}</h2>-->
        <!--<h2 :class="active">{{message}}</h2>-->
        
        用法2:
        <!--<h2 v-bind:class="{key1: value1, key2: value2}">{{message}}</h2>-->
        <!--<h2 v-bind:class="{类名1: Boolean, 类名2: Boolean}">{{message}}</h2>-->
        <!--当Boolean值为true时,该类名会被赋上该标签的class属性-->
        <!--<h2 v-bind:class="{active: isActive, line: isLine}">{{message}}</h2>-->
        
        用法3:
        <!--<h2 class="title" v-bind:class="{active: isActive, line: isLine}">{{message}}</h2>-->
        <!--不冲突 直接合并-->
        
        用法4:
        <h2 v-bind:class="getClasses()">{{message}}</h2>
        <button v-on:click="btnClick">按钮</button>
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                message: 'Azhang',
                isActive: true,
                isLine: true
            },
            methods: {
                btnClick: function(){
                    this.isActive = !this.isActive
                },
                getClasses: function(){
                    return {active: this.isActive, line: this.isLine}
                }
            }
        })
    </script>
</body>

v-bind——动态绑定class(数组语法) ’用的少‘

<body>
    <div id="app">
        <!--<h2 v-bind:class="['active', 'line']">{{message}}</h2>-->
        <h2 v-bind:class="[active, line]">{{message}}</h2>
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                message: 'Azhang',
                active: 'aaaaa',
                line: 'bbbbbb'
            }
        })
    </script>
</body>

v-bind——动态绑定style(对象语法)

<body>
    <div id="app">
        <!--<h2 v-bind:style="{fontSize: '50px'}">{{message}}</h2>-->
        <!--css属性名需用驼峰表示法或者使用font-size表示,属性值若不是变量需用单引号括住-->
        <!--<h2 v-bind:style="{fontSize: finalSize}">{{message}}</h2>-->
        <!--<h2 v-bind:style="{fontSize: finalSize + 'px'}">{{message}}</h2>-->
        <!--<h2 v-bind:style="{fontSize: finalSize + 'px', backgroundColor: finalColor}">{{message}}</h2>-->
        <h2 v-bind:style="getStyles()">{{message}}</h2>
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                message: 'Azhang',
               // finalSize: '100px'
                finalSize: 100,
                finalColor: 'red'
            },
            methods: {
                getStyles: function(){
                return {fontSize: this.finalSize + 'px', backgroundColor: this.finalColor}
                }
            }
        })
    </script>
</body>

v-bind——动态绑定style(数组语法)'用的少'

<body>
    <div id="app">
        <h2 v-bind:style="[baseStyle, baseStyle1]">{{message}}</h2>
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                message: 'Azhang',
                baseStyle: {backgroundColor: 'red'},
                baseStyle1: {fontSize: '100px'}
            }
        })
    </script>
</body>

04-计算属性

计算属性的基本使用

<body>
    <div id="app">
        <h2>{{firstName + ' ' + lastName}}</h2>
        <h2>{{firstName}} {{lastName}}</h2>
        <h2>{{getFullName()}}</h2>
        <h2>{{fullName}}</h2>
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                firstName: 'Lebron',
                lastName: 'James'
            },
            computed: {
                fullName: function(){
                    return this.firstName + ' ' + this.lastName
                }
            },
            methods: {
                getFullName(){
                    return this.firstName + ' ' + this.lastName
                }
            }
        })
    </script>
</body>

计算属性的复杂操作

<body>
    <div id="app">
        <h2>总价格:{{totalPrice}}</h2>    
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                books: [
                    {id: 110, name: 'unix编程艺术', price: 119},
                    {id: 111, name: '代码大全', price: 105},
                    {id: 112, name: '深入理解计算机原理', price: 98},
                    {id: 113, name: '现代操作系统', price: 87}
                ]
            },
            computed: {
                totalPrice: function(){
                    let result = 0
                    for(let i=0, i<this.books.length; i++){
                        result += books[i].price
                    }
                    return result
                    
                    //for(let i in this.books){
                    //    this.books[i]
                    //}
                    //for(let book of this.books){
                    //    
                    //}
                }
            }
        })
    </script>
</body>

计算属性的setter和getter

<body>
    <div id="app">
        <h2>总价格:{{totalPrice}}</h2>    
    </div>
    
    <script>
        const app = new vue({
            el: '#app',
            data: {
                firstName: 'kobe',
                lastName: 'bryant'
            },
            computed: {
                //fullName: function(){
                //    return this.firstName + ' ' + this.lastName
                //}
                
                //本质上是这样的:
                fullName: {
                    //set: function(){
                    //
                    //}
                    get: function(){
                        return this.firstName + ' ' + this.lastName
                    }
                }
                //但是一般情况下没有set方法,所以get也可以直接省略写成之前那样
                //set方法使用时需要有参数
                fullName: {
                    set: function(newValue){
                    //这个newValue是该计算属性被调用时所传进来的参数
                    }
                    get: function(){
                        return this.firstName + ' ' + this.lastName
                    }
                }
            }
        })
    </script>
</body>

jQuery_Ajax

  1. 一般格式:

    $.ajax({
         type: 'POST',
         url: url ,
         data: data ,
         success: success ,
         dataType: dataType
    });
  2. 方法参数表(红色为常用参数):(前六个为常用)

    名称                            值/描述
    type                        规定请求的类型(GET 或 POST)。
    url                            规定发送请求的 URL。默认是当前页面。
    success(result,status,xhr)    当请求成功时运行的函数。
    data                        规定要发送到服务器的数据。
    error(xhr,status,error)        如果请求失败要运行的函数。
    contentType                        发送数据到服务器时所使用的内容类型。默认                                        是:"application/x-www-form-urlencoded"。
    
    async                        布尔值,表示请求是否异步处理。默认是 true。
    beforeSend(xhr)                发送请求前运行的函数。
    cache                        布尔值,表示浏览器是否缓存被请求页面。默认是 true。
    complete(xhr,status)         请求完成时运行的函数(在请求成功或失败之后均调用,即在success和 error 函数之后)。
    context                        为所有 AJAX 相关的回调函数规定 "this" 值。
    dataFilter(data,type)        用于处理 XMLHttpRequest 原始响应数据的函数。
    dataType                    预期的服务器响应的数据类型。
    global                        布尔值,规定是否为请求触发全局 AJAX 事件处理程序。默认是 true。
    ifModified                    布尔值,规定是否仅在最后一次请求以来响应发生改变时才请求                            成功。默认是 false。
    jsonp                        在一个 jsonp 中重写回调函数的字符串。
    jsonpCallback                在一个 jsonp 中规定回调函数的名称。
    password                    规定在 HTTP 访问认证请求中使用的密码。
    processData                    布尔值,规定通过请求发送的数据是否转换为查询字符串。默认是 true。
    scriptCharset                规定请求的字符集。
    timeout                        设置本地的请求超时时间(以毫秒计)。
    traditional                    布尔值,规定是否使用参数序列化的传统样式。
    username                    规定在 HTTP 访问认证请求中使用的用户名。
    xhr                            用于创建 XMLHttpRequest 对象的函数。
  3. 一些需要注意的地方

    • data主要方式有三种,html拼接的,json数据,form表单经serialize()序列化的;通过dataType指定,不指定智能判断。
    • $.ajax只提交form以文本方式,如果异步提交包含\<file>上传是上传不过去的,需要使用jquery.form.js的$.ajaxSubmit
  4. 实例:

    //1.$.ajax带json数据的异步请求  
    var aj = $.ajax( {    
        url:'productManager_reverseUpdate',// 跳转到 action    
        data:{    
                 selRollBack : selRollBack,    
                 selOperatorsCode : selOperatorsCode,    
                 PROVINCECODE : PROVINCECODE,    
                 pass2 : pass2    
        },    
        type:'post',    
        cache:false,    
        dataType:'json',    
        success:function(data) {    
            if(data.msg =="true" ){    
                // view("修改成功!");    
                alert("修改成功!");    
                window.location.reload();    
            }else{    
                view(data.msg);    
            }    
         },    
         error : function() {    
              // view("异常!");    
              alert("异常!");    
         }    
    });  
      
      
    //2.$.ajax序列化表格内容为字符串的异步请求  
    function noTips(){    
        var formParam = $("#form1").serialize();//序列化表格内容为字符串    
        $.ajax({    
            type:'post',        
            url:'Notice_noTipsNotice',    
            data:formParam,    
            cache:false,    
            dataType:'json',    
            success:function(data){    
            }    
        });    
    }    
      
      
    //3.$.ajax拼接url的异步请求  
    var yz=$.ajax({    
         type:'post',    
         url:'validatePwd2_checkPwd2?password2='+password2,    
         data:{},    
         cache:false,    
         dataType:'json',    
         success:function(data){    
              if( data.msg =="false" ) //服务器返回false,就将validatePassword2的值改为pwd2Error,这是异步,需要考虑返回时间    
              {    
                   textPassword2.html("<font color='red'>业务密码不正确!</font>");    
                   $("#validatePassword2").val("pwd2Error");    
                   checkPassword2 = false;    
                   return;    
               }    
          },    
          error:function(){}    
    });   
      
      
    //4.$.ajax拼接data的异步请求  
    $.ajax({     
        url:'<%=request.getContextPath()%>/kc/kc_checkMerNameUnique.action',     
        type:'post',     
        data:'merName='+values,     
        async : false, //默认为true 异步     
        error:function(){     
           alert('error');     
        },     
        success:function(data){     
           $("#"+divs).html(data);     
        }  
    });  
  5. 更多实例:https://www.csdn.net/gather_2e/OtTaEg3sNjQwLWJsb2cO0O0O.html
  6. 获取输入框的值:https://blog.csdn.net/qq_36330733/article/details/88322071

jQuery比较是否包含字符串

  1. 使用indexOf()和lastIndexOf()方法

    案例:

    var Cts = "bblText";
    if(Cts.indexOf("Text") >= 0 ) {
        alert('Cts中包含Text字符串');
    }

    indexOf用法:

    
    返回 String 对象内第一次出现子字符串的字符位置。
    strObj.indexOf(subString[, startIndex])
    
    参数
    strObj
    
    必选项。String 对象或文字。
    subString
    
    必选项。要在 String 对象中查找的子字符串。
    starIndex
    
    可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。
    
    说明
    indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。
    
    如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。
    
    从左向右执行查找。否则,该方法与 lastIndexOf 相同。

    注意:

    indexOf() 方法对大小写敏感!
    如果要检索的字符串值没有出现,则该方法返回 -1。
    lastIndexOf() 的用法与indexOf()相同,只是是从右想左查找。
  2. 方法二:使用test()方法

    实例:在下面的例子中,我们将检索“W3School”:

    var str = "Visit W3School";
    var patt1 = new RegExp("W3School");
    var result = patt1.test(str);
    document.Write("Result: " + result);

    结果输出:Result: true

    使用方法介绍:

    
    定义和用法
    test() 方法用于检测一个字符串是否匹配某个模式.
    
    语法
    RegExpObject.test(string)
    
    参数  描述
    string  必需。要检测的字符串。
    
    返回值
    如果字符串 string 中含有与 RegExpObject 匹配的文本,则返回 true,否则返回 false。
    
    说明
    调用 RegExp 对象 r 的 test() 方法,并为它传递字符串 s,与这个表示式是等价的:(r.exec(s) != null)。

判断对象中是否含有某个key值