将输入字段的宽度调整为其输入 [英] Adjust width of input field to its input

查看:93
本文介绍了将输入字段的宽度调整为其输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
  <head>
  </head>
  <body>
    <input type="text" value="1" style="min-width:1px;" />
  </body>
</html>

这是我的代码,它不工作。在HTML,JavaScript,PHP或CSS中是否有其他方法来设置最小宽度?

This is my code and it is not working. Is there any other way in HTML, JavaScript, PHP or CSS to set minimum width?

Marcel的澄清:OP想要一个文本输入

Clarification by Marcel: The OP wants a text input field with a dynamically changing width, so that the input field fluids around its contents.

推荐答案

这听起来像你的期望是,样式将基于文本框的内容动态应用于文本框的宽度。如果是这样,您将需要一些js来运行文本框内容更改,类似这样:

It sounds like your expectation is that the style be applied dynamically to the width of the textbox based on the contents of the textbox. If so you will need some js to run on textbox contents changing, something like this:

<input id="txt" type="text" onkeypress="this.style.width = ((this.value.length + 1) * 8) + 'px';">

注意:这个解决方案只有在每个字符都是 8px wide。

Note: this solution only works when every character is exactly 8px wide.

这篇关于将输入字段的宽度调整为其输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆