如何使用 jquery 调整文本框大小并获取 div 元素内的尺寸

发布于 2024-12-12 05:26:18 字数 595 浏览 3 评论 0原文

我有一个可以拖动的 div 元素,并且该 div 元素内有一个文本框,当我自动拖动 div 元素时,文本框大小应随 div 一起增加。我需要获取文本框的 X 和 Y 位置,那么如何做我这样做?

这就是我现在所拥有的:

http://jsfiddle.net/wWFpP/3/

这是我的ode:

   <div class="demo">
            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
        </div>

这是我的拖动脚本:

<script>
        $(function () {
            $('.demo')
        .draggable()
        .resizable();
        });

    </script>

I have a div element which can be draggable and I have a textbox inside that div element and when I drag the div element automatically the textbox size should increase along with the div.And I need to get the textbox X and Y positions so how do I do that?

This is what I have now:

http://jsfiddle.net/wWFpP/3/

Here is my ode:

   <div class="demo">
            <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
        </div>

This is my script for dragging:

<script>
        $(function () {
            $('.demo')
        .draggable()
        .resizable();
        });

    </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2024-12-19 05:26:18

根据规范,textarea 应按行和列指定,但您仍然可以使用 CSS 对其进行样式设置:

#TextBox1
{
    width: 100%; height: 100%; /* make the element resize */
}
.demo
{
    width: 150px;
    height: 150px;
    padding: 5px 10px 20px 4px; /* updated padding to make things look better */
    background-color: #ff8811;
    position: absolute;
    top: 150px;
    left: 300px;
}

请参阅 这里。在 Firefox、Chrome 中测试。

另外,如果您需要获取 X 和 Y 坐标,draggable 有一个可以绑定到的 stop 方法:

$('.demo')
    .draggable({ stop: function(event, ui) {
        //get the textarea element and it's coordinates
        var txt = $(this).find('textarea:first');
        var x = txt.offset().left;
        var y = txt.offset().top;
        alert('(' + x + ', ' + y +')');
    } })
    .resizable();

A textarea should be specified in terms of rows and columns, according to the specs, but you can still style them with CSS:

#TextBox1
{
    width: 100%; height: 100%; /* make the element resize */
}
.demo
{
    width: 150px;
    height: 150px;
    padding: 5px 10px 20px 4px; /* updated padding to make things look better */
    background-color: #ff8811;
    position: absolute;
    top: 150px;
    left: 300px;
}

See here. Tested in Firefox, Chrome.

Also, if you need to get the X and Y coordinates, draggable has a stop method which you can bind to:

$('.demo')
    .draggable({ stop: function(event, ui) {
        //get the textarea element and it's coordinates
        var txt = $(this).find('textarea:first');
        var x = txt.offset().left;
        var y = txt.offset().top;
        alert('(' + x + ', ' + y +')');
    } })
    .resizable();
时光病人 2024-12-19 05:26:18

这就是你想要的吗?

http://jsfiddle.net/gmrcn/

编辑:
http://jsfiddle.net/gmrcn/2/

#TextBox1 {
    width: 100%;
    height: 100%; /* edit: fixed @ comment */
}

Is that what you want?

http://jsfiddle.net/gmrcn/

Edit:
http://jsfiddle.net/gmrcn/2/

#TextBox1 {
    width: 100%;
    height: 100%; /* edit: fixed @ comment */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文