jquery框架和mvvm框架的類名操做性對比

操做元素選取的是img元素css

mvvm選取的是avalon框架html

操做類名上感受仍是jquery有着很強的優點,畢竟有着toggle方法jquery

mvvm上實現相同效果還得作邏輯判斷,比較麻煩,代碼行數就多了幾行。bootstrap

點擊看效果demo框架

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="./demo.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
    <script src="https://unpkg.com/avalon2@2.2.8/dist/avalon.js"></script>
    <style>
        .clickClass{
            padding: 5px;
            border: 2px solid #e4e4e4;
            background: #868e96;
        }
    </style>
</head>
<body>
    <div class="demo-container">
        <div class="header">
            <i><b>jquery和mvvm的類名操做比較</b></i>
        </div>
        <div class="method-title">jq方法</div>
        <div class="method-container">
           <img id="img1" src=".././images/imgcommon1.jpg">
        </div>
        <div class="method-title">mvvm方法</div>
        <div class="method-container" :controller="img2">
            <img  ms-click="@clickImg" id="img2" :class="@className" src=".././images/imgcommon1.jpg">
        </div>
    </div>
    <script>

            //jq way
            $('#img1').click(function(e) {
                // $(this).addClass('clickClass');
                $(this).toggleClass('clickClass');
            });

            //mvvm way
            let mvvmImg = avalon.define({
                $id: 'img2',
                className: '',
                clickImg(e) {
                    this.className = 'clickClass';
                }
            });      
    </script>
</body>
</html>
相關文章
相關標籤/搜索