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

时间:2009-06-05 16:43:20

标签: html css xhtml stylesheet

我想以“弹性”或“流动”的方式将一个文本框和一个按钮放在一起(正确的术语是什么?),如下所示:

layout
(来源:ocactus.org

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

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

4 个答案:

答案 0 :(得分:8)

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

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

可能有&lt; div&gt;替代那里的造型。

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

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

答案 1 :(得分:0)

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

input {width:50%}

<input>
<button>save</button>

答案 2 :(得分:0)

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>

注意:请务必在文档的&lt; head&gt;中包含jQuery库。为了提高速度,我建议使用Google托管的版本:http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js

答案 3 :(得分:0)

这是我根据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);
相关问题