简体   繁体   English

如何在HTML中移动占位符文本和文本框的内容?

[英]How to move placeholder text along with the content of a textbox in HTML?

Suppose this is my textbox: 假设这是我的文本框:

<input type="text" placeholder="%" />

And a user is supposed to enter a percentage inside, but without the % sign, only the numbers (eg 67 in 67%). 并且用户应该在内部输入百分比,但是没有%符号,只输入数字(例如,67%中的67)。 But I want them to still remember that this is a text box in which you insert a percentage. 但我希望他们仍然记得这是一个插入百分比的文本框。

So how can I move the placeholder along with the text, make it unable to be deleted, always after the text? 那么如何将占位符与文本一起移动,使其无法删除,始终在文本之后?

And I do remember seeing it somewhere too, unless I got my facts wrong. 我确实记得在某个地方看到它,除非我弄错了事实。

A way to do this would be to have an additional element overlaying the input element and moving the overlayed element as the user types. 一种方法是使用一个额外的元素覆盖输入元素,并在用户输入时移动重叠的元素。

But, I think a better UX experience would be to have the element as an add-on appended to the input field, as show in twitter bootstrap. 但是,我认为更好的用户体验体验是将元素作为附加到输入字段的附加组件,如twitter bootstrap中所示。 See the "extending form controls" settings: 请参阅“扩展表单控件”设置:

http://twitter.github.com/bootstrap/base-css.html#forms http://twitter.github.com/bootstrap/base-css.html#forms

You could simulate an input and change the width of the real input using javascript. 您可以使用javascript模拟输入并更改实际输入的宽度。 (The trick is to use some invisible element to catch the needed width) (诀窍是使用一些不可见的元素来捕获所需的宽度)

Exemple using JQuery: http://jsfiddle.net/Vu7hN/ 使用JQuery的例子: http//jsfiddle.net/Vu7hN/

$input.on('change keypress paste focus textInput input', function(){
    testWidth.text($input.val());
    $input.width(testWidth.width());
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用存储在本地存储中的文本替换 HTML 中的替换 {{content}} 占位符 - How to replace Replace {{content}} placeholder in HTML with text stored in local storage 在文本框中键入文本时,移动默认文本 - Move a default text as you type in a textbox along with text typed 如何在单击按钮时使用AngularJs将文本框的内容移动到另一个文本框? - How to move the content of a textbox into another textbox using AngularJs on button click? 如何在调整窗口大小的同时移动内容,使其出现在相同的位置? - How to move content along with window resize so that it appears in the same position? 如何沿着DOM移动iframe而不丢失它的内容? - How to move iframe along the DOM without losing it's content? 如何隐藏 html 文本框中的文本 - how to hide text in an html textbox 如何在 select 中将占位符文本向右移动几个像素? - How to move placeholder text few pixels to the right in select? 将 div 中的内容连同样式组件一起导出到 text/html 文件 - Exporting content within div along with styled components to text/html file 如何将变量值与 in.html() 中的文本一起显示 - how to display a variable value along with the text in .html() 如何通过使用javascript的html select移动html内容 - how to move html content by html select with javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM