jQuery resizable只适用于第一个元素

时间:2010-11-09 14:29:33

标签: jquery jquery-ui resizable jquery-ui-resizable jquery-resizable

我的页面上的控件将包含多个需要调整大小的多行文本框 我试图使用jQuery resizable函数来完成此任务。我只需要我的文本框可以垂直扩展。不幸的是,我只能获得可调整大小的功能来为第一个div工作。

以下是我的代码示例:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Page_jQuery_Resizable.aspx.vb"
    Inherits="jQueryTest.Page_jQuery_Resizable" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>jQuery Test - Page 2</title>
    <script src="Scripts/js/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="Scripts/js/jquery-ui-1.8.6.custom.min.js" type="text/javascript"></script>
</head>
<body>
    <style type="text/css">
        table.textboxTable td
        {
            padding: 15px;
            padding-bottom: 30px;
            border: 2px solid blue;
        }
        .ImResizable
        {
            height: 60px;
            width: 470px;
        }
        .ImResizable textarea
        {
            height: 95%;
            width: 100%;
        }
        .ui-resizable-handle
        {
            height: 8px;
            width: 474px;
            background: #EEEEEE url('Images/grippie.png') no-repeat scroll center;
            border-color: #DDDDDD;
            border-style: solid;
            border-width: 0pt 1px 1px;
            cursor: s-resize;
        }
    </style>
    <form id="form1" runat="server">

    <table class="textboxTable">
        <tr>
            <td>
                <div class="ImResizable">
                    <asp:TextBox runat="server" TextMode="MultiLine" />
                    <div class="ui-resizable-handle" />
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div class="ImResizable">
                    <asp:TextBox runat="server" TextMode="MultiLine" />
                    <div class="ui-resizable-handle" />
                </div>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
<script type="text/javascript">
    $(function ()
    {
        $("div.ImResizable").resizable(
        {
            minHeight: 25,
            maxHeight: 85,
            minWidth: 470,
            maxWidth: 470,
            handles: { 's': $("div.ui-resizable-handle") }
        });
    });
</script>

alt text

4 个答案:

答案 0 :(得分:3)

您可以使用$ .each并迭代元素。像这样:

$("div.ImResizable").each(function() {
    var $this = $(this);
    $this.resizable({
        minHeight: 25,
        maxHeight: 85,
        minWidth: 470,
        maxWidth: 470,
        handles: { 's': $this.find("div.ui-resizable-handle") }
    });
});

答案 1 :(得分:0)

您正在寻找jQuery.each功能。

答案 2 :(得分:0)

正确如上。你需要遍历类的每个元素,并分配子或相关的句柄,你现在的方式,你正在分配div.ui-resizable-handle的泛型类作为句柄div.ImResizable的泛型类元素。它们没有一对一的匹配。

答案 3 :(得分:0)

基本上,如果您一次将Resizable应用于多个对象,则需要为要用作句柄的子元素传递选择器。此外,每个要调整大小的元素都必须相同。

Example

来自jQueryUI网站的引用。大胆增加强调。 http://api.jqueryui.com/resizable/#option-handles

  

对象:   支持以下键:{n,e,s,w,ne,se,sw,nw}。任何指定的值应该是 jQuery选择器,匹配resizable的子元素以用作该句柄。如果句柄是 NOT 可调整大小的子句,则可以直接传入DOMElement或有效的jQuery对象。注意:生成自己的句柄时,每个句柄必须具有ui-resizable-handle类以及相应的ui-resizable- {direction}类,.eg,ui-resizable -s

HTML:

<table class="textboxTable">
    <tr>
        <td>
            <div class="ImResizable">
                <asp:TextBox runat="server" TextMode="MultiLine" />
                <div class="ui-resizable-handle" />
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <div class="ImResizable">
                <asp:TextBox runat="server" TextMode="MultiLine" />
                <div class="ui-resizable-handle ui-resizable-s" />
            </div>
        </td>
    </tr>
</table>

Javascript:

$(function ()
{
    $("div.ImResizable").resizable(
    {
        minHeight: 25,
        maxHeight: 85,
        minWidth: 470,
        maxWidth: 470,
        handles: { 's': "div.ui-resizable-handle" }
    });
});
相关问题