【问题标题】:How to position a liquid/elastic textbox next to a button?如何在按钮旁边放置一个液体/弹性文本框?
【发布时间】:2009-06-05 16:43:20
【问题描述】:

我想以“弹性”或“液体”方式(正确的术语是什么?)将一个文本框和一个按钮放置在彼此旁边,如下所示:


(来源:ocactus.org

当放置在任意宽度的容器中(包括调整浏览器窗口大小)时,按钮应右对齐并占用所需的宽度,而文本框应使用剩余宽度。不幸的是,按钮的宽度不能在 CSS 中固定,因为它取决于标题(不同的操作、语言等)。

对于上述问题,有什么好的跨浏览器解决方案?

【问题讨论】:

    标签: html css xhtml stylesheet


    【解决方案1】:

    我能够让它在一个表格中工作(我知道,但它可以工作),它可以正确处理页面大小调整以及按钮值的变化:

    <table class="elastic">
        <tr>
            <td><input class="textbox" type="text"></td>
            <td class="button"><input type="button" value="Test Button"></td>
        </tr>
    </table>
    

    可能有一个

    替代样式用于样式。

    编辑:我修改了我的示例以使用以下样式表:

    .elastic { width:100%; }
    .elastic .textbox { width: 100%; }
    .elastic .button { width: 1%; }
    

    【讨论】:

    • 我试图避免使用表格,但我想这是实现我想要的最好/唯一的方法。谢谢。
    【解决方案2】:

    如果不看一些代码,很难给出明确的答案,但以下代码会将输入的大小调整为浏览器宽度的 50%,并带有旁边的按钮。

    input {width:50%}
    
    <input>
    <button>save</button>
    

    【讨论】:

    • ..但我希望文本框是“可用宽度的其余部分”,而不是浏览器宽度的固定百分比?
    【解决方案3】:

    HTML:

    <div class="widebox">
      <input type="text" value="" />
      <button>Button</button>
    </div>
    

    jQuery:

    <script type="text/javascript">
    $(document).ready(function(){
      var w = $(".widebox");
      $(".widebox input:text").css({ width: (w.width - (w.children("button:first").width + 20)) });
    });
    </script>
    

    注意:请务必在文档的

    中包含 jQuery 库。为了速度,我建议使用 Google 托管的:http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js

    【讨论】:

      【解决方案4】:

      这是我根据 NGJ Graphics 的回答编写的 jQuery 扩展。它考虑了多个按钮,例如确定/取消选项。

      (function($) {
        $.fn.extend({
          widebox: function() {
            var el = $(this);
            var width = 0;
            el.children("button").each(function() {
              width += $(this).outerWidth( true );
            });
            el.children("input:text").css({ width: (el.width() - (width + 40)) });
          }
        });
      })(jQuery);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 2019-01-02
        • 1970-01-01
        • 1970-01-01
        • 2015-01-10
        • 1970-01-01
        相关资源
        最近更新 更多