代码之家  ›  专栏  ›  技术社区  ›  Josef Pfleger

如何在按钮旁边放置液体/弹性文本框?

  •  8
  • Josef Pfleger  · 技术社区  · 17 年前

    我想以“弹性”或“液体”的方式将文本框和按钮放在一起(正确的术语是什么?)像这样:

    layout http://www.ocactus.org/liquid.gif

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

    对于上述跨浏览器工作的解决方案,什么是好的解决方案?

    4 回复  |  直到 17 年前
        1
  •  8
  •   CAbbott    17 年前

    我可以让它在一个表中工作(我知道,但它可以工作),在这个表中它可以正确地处理页面大小调整以及按钮更改的值:

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

    可能有一个<DIV>替代样式。

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

    .elastic { width:100%; }
    .elastic .textbox { width: 100%; }
    .elastic .button { width: 1%; }
    
        2
  •  0
  •   Emily    17 年前

    在没有看到某些代码的情况下很难给出明确的答案,但下面的内容将使用旁边的按钮将输入的浏览器宽度调整为50%。

    input {width:50%}
    
    <input>
    <button>save</button>
    
        3
  •  0
  •   NGJ Graphics    16 年前

    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>
    

    注意:请确保在文档的<head>中包含jquery库。对于速度,我建议使用Google托管的: http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js

        4
  •  0
  •   Andrew De Andrade    15 年前

    这是我根据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);