简体   繁体   English

当输入中包含文本时,如何在元素上切换类?

[英]How can you toggle a class on an element when an input has text in it?

I'm just learning JavaScript and haven't found any vanilla JS solutions for this problem.我只是在学习 JavaScript,还没有找到任何针对此问题的 vanilla JS 解决方案。 I'm working on a floating label form group, where the label for an input is 'tucked away' behind the input.我正在处理一个浮动标签表单组,其中输入的标签“隐藏”在输入后面。 I want it to rise when the input has text in it by toggling the class 'form-group-with-value'.我希望它通过切换类“form-group-with-value”在输入中有文本时上升。 I don't know any jQuery and I'd like to avoid it.我不知道任何 jQuery,我想避免它。 Here's my code: PS: I've managed to toggle a class when an input is in focus (to change the color of the label).这是我的代码: PS:当输入处于焦点时(以更改标签的颜色),我设法切换了一个类。 Although it's really not the best solution, it works as is.虽然它确实不是最好的解决方案,但它按原样工作。

 //Dynamic Form Label const input = document.querySelectorAll('.custom-input'); const formGroup = document.querySelectorAll('.form-group'); //Toggle color when input is in focus input[0].addEventListener('focus', () => formGroup[0].classList.toggle('form-group-with-focus') ); input[0].addEventListener('blur', () => formGroup[0].classList.toggle('form-group-with-focus') ); input[1].addEventListener('focus', () => formGroup[1].classList.toggle('form-group-with-focus') ); input[1].addEventListener('blur', () => formGroup[1].classList.toggle('form-group-with-focus') ); input[2].addEventListener('focus', () => formGroup[2].classList.toggle('form-group-with-focus') ); input[2].addEventListener('blur', () => formGroup[2].classList.toggle('form-group-with-focus') ); input[3].addEventListener('focus', () => formGroup[3].classList.toggle('form-group-with-focus') ); input[3].addEventListener('blur', () => formGroup[3].classList.toggle('form-group-with-focus') ); //Toggle label position when input has value
 .form-group{ position: relative; min-height: 50px; } .form-group label{ position: absolute; z-index: -1; transition: 0.3s; left: 5px; } .form-group-with-focus label{ color: red; } .form-group-with-value label{ transform: translateY(-80%); }
 <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div> <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div> <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div> <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div>

have mercy pls, I'm a beginner请怜悯我,我是初学者

To simplify your code, you can loop through the inputs and generate a single event handler instead of multiple.为了简化您的代码,您可以遍历输入并生成单个事件处理程序而不是多个事件处理程序。 Plus you can call the input's parent node to toggle the class on.另外,您可以调用输入的父节点来打开类。

Plus you don't want to toggle if you only want the label to show if the input has value.另外,如果您只希望标签显示输入是否具有值,则您不想切换。

I chose the input event as it covers paste as well as keyboard entry.我选择了input事件,因为它涵盖了粘贴和键盘输入。

 //Dynamic Form Label const input = document.querySelectorAll('.custom-input'); input.forEach(function(el) { el.addEventListener('input', function(e) { if (e.target.value == "") { e.target.parentNode.classList.remove('form-group-with-value') } else { e.target.parentNode.classList.add('form-group-with-value') } }); });
 .form-group { position: relative; min-height: 50px; } .form-group label { position: absolute; z-index: -1; transition: 0.3s; left: 5px; } .form-group-with-focus label { color: red; } .form-group-with-value label { transform: translateY(-80%); }
 <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div> <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div> <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div> <div class="form-group "> <label>Full Name</label> <input type="text" class="form-control custom-input" id="name" placeholder="Full Name"> </div>

如果您希望在用户键入时调用某些 JS/Function,您可以使用keyPresskeyUp事件。

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

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