大佬教程收集整理的这篇文章主要介绍了jquery如果输入文本有值,则将类添加到label,如果没有值则删除它,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
代码到目前为止:
@H_607_9@<div class="input-wrap"> <input type="text" name="textdemo" id="textdemo" size="20" value="" class="textdemo" /> <label class="demo-label" for="textdemo">{L_USERNAME}</label> </div> <script type="text/javascript"> jQuery(document).ready(function(){ $('.input-wrap .textdemo').val() ) { $('label.demo-label').addClass('input-has-value'); } }); </script>
@H_607_9@function checkForInput(element) { // element is passed to the function ^ const $label = $(element).siblings('label'); if ($(element).val().length > 0) { $label.addClass('input-has-value'); } else { $label.removeClass('input-has-value'); } } // The lines below are executed on page load $('input.textdemo').each(function() { checkForInput(this); }); // The lines below (insidE) are executed on change & keyup $('input.textdemo').on('change keyup',function() { checkForInput(this); }); @H_607_9@label.input-has-value { color: red; } @H_607_9@<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="input-wrap"> <input type="text" name="textdemo" id="textdemo" size="20" value="" class="textdemo" /> <label class="demo-label" for="textdemo">{L_USERNAME}</label> </div>
以上是大佬教程为你收集整理的jquery如果输入文本有值,则将类添加到label,如果没有值则删除它全部内容,希望文章能够帮你解决jquery如果输入文本有值,则将类添加到label,如果没有值则删除它所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。