如何设置输入宽度以匹配占位符文本宽度 [英] How to set an input width to match the placeholder text width

查看:357
本文介绍了如何设置输入宽度以匹配占位符文本宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例 http://dabblet.com/gist/5859946

如果您有一个长长的占位符,默认情况下,输入的宽度不足以显示所有占位符文本。

If you have a long placeholder, the input by default does not display wide enough to show all the placeholder text.

例如

<input placeholder="Please enter your name, address and shoe size">

没有设置固定宽度,最好没有javascript,可以设置输入以显示所有占位符

Without setting a fixed width, and preferably no javascript, can you set the input to show all the placeholder text?

推荐答案

这只能通过javascript设置size =占位符长度来完成:

This can be done only via javascript by setting the size = placeholder length:

input.setAttribute('size',input.getAttribute('placeholder').length);

这里是所有输入的代码:
http://jsfiddle.net/KU5kN/

Here is a code that dose that for all the inputs: http://jsfiddle.net/KU5kN/

这篇关于如何设置输入宽度以匹配占位符文本宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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