简体   繁体   English

我需要在聚焦输入时隐藏占位符,而是显示该输入的 label

[英]I need to hide a placeholder when focusing an input but showing instead the label of that input

I have been looking for how to fix this, I have applied scss and css, also javascript.我一直在寻找如何解决这个问题,我已经应用了 scss 和 css,还有 javascript。 I need my input to show a placeholder when it opens normally and when you focus on it it will display its label and look small as in this photo I have.我需要我的输入来显示一个占位符,当它正常打开时,当你专注于它时,它会显示它的 label 并且看起来像我的这张照片一样小。

https://i.stack.imgur.com/VxEqE.jpg https://i.stack.imgur.com/VxEqE.jpg

This is the input code and the label I'm handling serves me any code, ts, js, css, html.这是输入代码,我正在处理的 label 为我提供任何代码、ts、js、css、html。

<label> Contraseña </label>
<input placeholder='Ingrese su contraseña...'                           [class.isinvalid]="loginForm.controls.contrasena.invalid && (loginForm.controls.contrasena.dirty || loginForm.controls.contrasena.touched || isSubmited)">
<div *ngIf="loginForm.controls.contrasena.invalid && (loginForm.controls.contrasena.dirty || loginForm.controls.contrasena.touched || isSubmited)">
<small *ngIf="loginForm.controls.contrasena.hasError('required')">La contraseña es requerido</small>
</div>
</div>

You can find this in angular material library, if you are planning to use in your project with more options.如果您打算在项目中使用更多选项,您可以在 angular 材料库中找到它。 https://material.angular.io/components/form-field/overview#form-field-overview https://material.angular.io/components/form-field/overview#form-field-overview

Else, you can build your custom input from this post.否则,您可以从这篇文章构建您的自定义输入。
Move placeholder above the input on focus 将占位符移到焦点上的输入上方

you don't have to hide the placeholder & you can't.你不必隐藏占位符,你不能。 you can do it easily --你可以轻松做到——

  1. transform the label a bit down to the exact location of placeholder.将 label 稍微转换为占位符的确切位置。
  2. increase the font-size of label.增加 label 的字体大小。
  3. set focus state of input set the transform of label to normal and decrease the font-size of the label.设置输入焦点 state 将 label 的变换设置为正常并减小 label 的字体大小。
  4. to provide transition to this whole process use transition property in css.要提供到整个过程的转换,请使用 css 中的转换属性。
  5. to make everything work perfectly add a click handler to the label so that whenever it is clicked it focuses the input.为了使一切正常工作,向 label 添加一个单击处理程序,以便在单击它时聚焦输入。 document.getElementById("mytext").focus();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM