简体   繁体   中英

Jquery - Button to add Panel / Button to Remove Panel

How could I add two buttons to the below? The first button would add a panel and the second would remove a selected panel?

The idea being that I can have as many panels as I want and remove certain ones if I wish.

<!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>

You could add 2 buttons in your HTML and attach a function.

For add it appends the draggable div to your body. You have to use ID instead of class to select the element.

Here is the code working for adding a draggable 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM