【问题标题】:JQuery Add Multiple Re-sizable Divs on a button clickJQuery 在单击按钮时添加多个可调整大小的 Div
【发布时间】:2014-02-26 20:43:29
【问题描述】:

有一些关于如何使用 JQuery 制作可调整大小的 div 的帖子。我正在使用 asp.net 并使用了几篇文章中的代码来执行此操作,这将在下面附加。 我现在需要做的是有一个按钮,当单击它时,我会得到另一个克隆的可拖动和可调整大小的 div,我还需要捕获创建的每个 div 的 x、y 宽度和高度。下面的代码为硬编码捕获这些值,并在一系列 asp.net 文本框中显示值。

为任何帮助和建议干杯

问候 厘米

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs"   
Inherits="Default3" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Cloned Divs</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-
ui.css" />
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
    #drag {
        width: 6em;
        height: 6em;
        padding: 0.5em;
        border: 3px solid #ccc;
        border-radius: 0 1em 1em 1em;
        background-color: #fff;
        background-color: rgba(255,255,255,0.5);
    }

    #Buttons {
        width: 50px;
        height: 50px;
        left: 50px;
        position: absolute;
    }
</style>
<script>
    $(function () {
        $("#drag").draggable(
            {
                containment: $('body'),
                drag: function () {
                    var offset = $(this).offset();
                    var xPos = offset.left;
                    var yPos = offset.top;
                    var myHeight = $("#draggable").height();
                    $('#posX').text('x: ' + xPos);
                    $('#posY').text('y: ' + yPos);
                    $("#<%=txtX.ClientID%>").val(xPos);
                    $("#<%=txtY.ClientID%>").val(yPos);
                }
            }).resizable({
                resize: function (event, ui) {
                    var mywidth = $(event.target).width();
                    var myHeight = $(event.target).height();
                    $('#width').text('width: ' + mywidth);
                    $('#height').text('height: ' + myHeight);
                    $("#<%=txtWidth.ClientID%>").val(mywidth);
                    $("#<%=txtHeight.ClientID%>").val(myHeight);
                }

            });
    });

        $('#btnAdd').click(function () {

            var structure = $('<div id="draggable1" class="ui-widget-content"></div>');
            $('#body').append(structure);

        });
</script>
</head>
<body>
<form id="form1" runat="server">
    <div id="drag" class="ui-widget-content">
        <ul>
            <li id="posX"></li>
            <li id="posY"></li>
            <li id="width"></li>
            <li id="height"></li>
        </ul>
    </div>
    <div id="Buttons">
        <asp:Label ID="lblX" runat="server" Text="X: "></asp:Label><asp:TextBox
 ID="txtX" runat="server"></asp:TextBox>
        <asp:Label ID="lblY" runat="server" Text="Y: "></asp:Label><asp:TextBox 
ID="txtY" runat="server"></asp:TextBox>
        <asp:Label ID="lblWidth" runat="server" Text="Width: "></asp:Label><asp:TextBox
ID="txtWidth" runat="server"></asp:TextBox>
        <asp:Label ID="lblHeight" runat="server" Text="Height: "></asp:Label>
<asp:TextBox ID="txtHeight" runat="server"></asp:TextBox>
        <asp:Button ID="btnAdd" runat="server" Text="Add Div" />
    </div>
</form>
</body>
</html>

【问题讨论】:

    标签: javascript jquery html asp.net css


    【解决方案1】:

    嗯,你有一些严重的 CSS 问题需要处理。我不进入这个问题,但我会回答你关于 div add 的问题。

    最简单的方法是将您的“$.draggable”事件转换为单独的方法,并通过类而不是 id 应用可拖动属性。然后在每个 div 添加时调用它。此外,要使用 JQuery 附加元素,请使用 $(document.body).append("")。

    最后,您不应该为此使用 asp:Button,但如果必须,请务必添加 OnClientClick="return false;" 属性,否则它将提交您的页面。

    以下是 javascript 调整:

    function ApplyDrag() {
        $(".draggable").draggable(
        {
            containment: $('body'),
            drag: function() {
                var offset = $(this).offset();
                var xPos = offset.left;
                var yPos = offset.top;
                var myHeight = $("#draggable").height();
                $('#posX').text('x: ' + xPos);
                $('#posY').text('y: ' + yPos);
                $("#<%=txtX.ClientID%>").val(xPos);
                $("#<%=txtY.ClientID%>").val(yPos);
            }
        }).resizable({
            resize: function(event, ui) {
                var mywidth = $(event.target).width();
                var myHeight = $(event.target).height();
                $('#width').text('width: ' + mywidth);
                $('#height').text('height: ' + myHeight);
                $("#<%=txtWidth.ClientID%>").val(mywidth);
                $("#<%=txtHeight.ClientID%>").val(myHeight);
            }
    
        });
    }
    
    $(function() {
        ApplyDrag();
    
        $('#<%=btnAdd.ClientID %>').click(function() {
            var structure = $('<div class="draggable ui-widget-content">BLABLA</div>');
            $(document.body).append(structure);
            ApplyDrag();
        });
    });
    

    【讨论】:

    • 这正是我想要的。非常感谢您的出色和快速响应。
    猜你喜欢
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多