用JQuery和angularjs分别实现两个盒子间按钮的跳转(第三版 demo实现)

董哥给的demo
其他自己的实现;

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    .box{
        background:#ccc;
        width:200px;
        height:200px;
        display:inline-block;
        position:relative;
    }
    .box span{
        float:left;
        padding:3px 10px;
        background:#555;
        color:#fff;
        margin:5px;
        font-size:12px;
        cursor:pointer;
    }
    .box .btn{
        position:absolute;
        bottom:0;
        width:100%;
        background:#999;
        text-align:center;
    }
</style>
<body>

<div class="box" id="box1">
    <div class="cont"></div>
    <div class="btn">
        <button>清空</button>
        <button>排序</button>
    </div>
</div>
<div class="box" id="box2">
    <div class="cont"></div>
    <div class="btn">
        <button>清空</button>
        <button>排序</button>
    </div>
</div>

<script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script>
    $(function(){
        //数据源
        var Data = {
            d1:['9','5','4','3','2'],
            d2:['7','8','1']
        };
        //分配数据
        var loadItem = function(data){
            var html = '';
            for(var i = 0, len = data.length; i < len; i++){
                html = html + '<span>'+ data[i]+'</span>'
            }
            return html;
        };

        var reload = function (){
            $('#box1 .cont').html(loadItem(Data.d1));
            $('#box2 .cont').html(loadItem(Data.d2));
        };

        //首次加载
        reload();
        //jquery事件绑定
        $('.box').on('click', function(e){
            var cell = e.target;         //当前点击的对象
            var txt = cell.innerText;    //当前对象的值

            //点击单元格
            if(cell.tagName === 'SPAN'){
                //判断单元格父级容器用来确定使用那个数据源
                //indexOf可以返回字符串首先出现的位置
                if(this.id === 'box1'){
                    Data.d1.splice(Data.d1.indexOf(txt), 1);
                    Data.d2.push(txt);
                }else{
                    Data.d2.splice(Data.d2.indexOf(txt), 1);
                    Data.d1.push(txt);
                }
                //每次点击重新加载
                console.log(Data.d1,Data.d2);
                reload();
            }

            //点击清空按钮
            if(cell.tagName === 'BUTTON' && cell.innerText === '清空'){
                if(this.id=='box1'){
                    var num = $("#box1 .cont").children();
                    $("#box1 .cont").empty;
                    $("#box2 .cont").append(num);
                }else{
                    var num = $("#box2 .cont").children();
                    $("#box2 .cont").empty;
                    $("#box1 .cont").append(num);
                }
            }

            //点击排序按钮
            if(cell.tagName === 'BUTTON' && cell.innerText === '排序'){
                var nwords = [];
                var words = [];
                var cmd = [] ;
                if(this.id=='box1'){
                    var words = $("#box1 .cont").children();
                    for(var i = 0;i<words.length;i++){
                        nwords[i] = words[i].innerHTML;//获取span中的内容 只能用innerHTML来获取
                    }
                    nwords.sort();
                    $("#box1 .cont").empty();
                    $("#box1 .cont").html(loadItem(nwords));
                }else{
                    var words = $("#box2 .cont").children();
                    for(var i = 0;i<words.length;i++){
                        nwords[i] = words[i].innerHTML;//获取span中的内容 只能用innerHTML来获取
                    }
                    nwords.sort();
                    $("#box2 .cont").empty();
                    $("#box2 .cont").html(loadItem(nwords));
                }

            }

        })
    });
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很高兴回答你的问题。您可以使用jQuery UI对话框组件来实现这个功能。具体步骤如下: 1. 首先,在html文件中引入jQueryjQuery UI库,例如: ```html <head> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> </head> ``` 2. 然后,在html文件中添加一个按元素,例如: ```html <button id="dialog-button">点击弹出对话框</button> ``` 3. 接下来,在JavaScript脚本中添加以下代码,来初始化对话框组件: ```javascript $(document).ready(function() { $("#dialog").dialog({ autoOpen: false, modal: true, buttons: { "确定": function() { $(this).dialog("close"); // 在这里添加点击确定时的操作 }, "取消": function() { $(this).dialog("close"); // 在这里添加点击取消时的操作 } } }); }); ``` 这个代码将会在文档加载完毕时执行,创建一个对话框组件,并设置它的自动弹出和模态对话框的属性。按定义在它的buttons属性中,点击"确定"按时执行第一个函数,点击"取消"按时执行第二个函数。 4. 最后,在JavaScript脚本中添加以下代码,来添加按的点击事件: ```javascript $("#dialog-button").click(function() { $("#dialog").dialog("open"); }); ``` 这个代码将会为按元素添加一个点击事件处理器,当按被点击时,调用dialog("open")方法来打开对话框组件。 5. 最后,在html文件中添加一个对话框元素,例如: ```html <div id="dialog" title="提示"> 是否确认? </div> ``` 这个代码将会在页面中添加一个对话框元素,它的id属性为"dialog",标题为"提示",内容为"是否确认?"。 完成上述步骤后,您就可以点击按,弹出一个有确定和取消按的对话框了。记得要先引入jQueryjQuery UI库,否则代码不会生效。希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值