Jquery - 用于添加面板/按钮以删除面板

时间:2017-05-17 13:46:15

标签: javascript jquery

我怎样才能在下方添加两个按钮?第一个按钮会添加一个面板,第二个按钮会删除一个选定的面板吗?

我的想法是,如果我愿意的话,我可以拥有尽可能多的面板,并删除某些面板。

<!DOCTYPE HTML>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<html lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css"/>   
<script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
        $('#drag').resizable({
            stop: function(event, ui) {
                var w = $(this).width();
                var h = $(this).height(); 
            }
        }).draggable(
            {
                containment: $('body'),
                drag: function(){
                    var offset = $(this).offset();
                    var xPos = offset.left;
                    var yPos = offset.top;
                    $('#posX').text('x: ' + xPos);
                    $('#posY').text('y: ' + yPos);
                },
                stop: function(){
                    var finalOffset = $(this).offset();
                    var finalxPos = finalOffset.left;
                    var finalyPos = finalOffset.top;
            $('#finalX').text('Final X: ' + finalxPos);
            $('#finalY').text('Final X: ' + finalyPos);
                }
            });
    });
</script>   
<style type="text/css">
    #drag {
        width: 16em;
        height: 16em;
        padding: 0.5em;
        border: 3px solid #000;
        background-color: #fff;
        background-color: rgba(255,255,255,0.5);
    }
</style>
</head>
<body>
  <div id="drag" class="panel panel-default">
    <div class="panel-body">Dragable & Sizable</div>
  </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您可以在HTML中添加2个按钮并附加功能。

对于添加,它会将可拖动的div添加到您的身体。您必须使用ID而不是类来选择元素。

以下是添加可拖动div的代码:

<!DOCTYPE HTML>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <html lang="en">
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>
        <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css"/>
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $("#add").click(function () {
                    $("body").append("<div class=\"drag panel panel-default\"><div class=\"panel-body\">Draggable & Sizable</div></div>" );
                    $(".drag").resizable({
                        stop: function(event, ui) {
                            var w = $(this).width();
                            var h = $(this).height();
                        }
                    }).draggable(
                        {
                            containment: $('body'),
                            drag: function(){
                                var offset = $(this).offset();
                                var xPos = offset.left;
                                var yPos = offset.top;
                                $('#posX').text('x: ' + xPos);
                                $('#posY').text('y: ' + yPos);
                            },
                            stop: function(){
                                var finalOffset = $(this).offset();
                                var finalxPos = finalOffset.left;
                                var finalyPos = finalOffset.top;
                                $('#finalX').text('Final X: ' + finalxPos);
                                $('#finalY').text('Final X: ' + finalyPos);
                            }
                        });
                });

            });
        </script>
        <style type="text/css">
            .drag {
                width: 16em;
                height: 16em;
                padding: 0.5em;
                border: 3px solid #000;
                background-color: #fff;
                background-color: rgba(255,255,255,0.5);
            }
        </style>
    </head>
    <body>
    <button id="add">Add</button>
    <button id="hide">Hide</button>
    </body>
    </html>