我渴望解决我的问题与自动生成输入跨度。我在我的网站上使用Wordpress插件联系表单7,我希望在用户使用输入字段的同时为标签制作动画。添加班级如果输入焦点和跨度
当输入字段处于焦点状态或其中键入的内容时,活动类应显示在具有“grid-3”类的div中。
我试图让自己的JavaScript,但它不工作:
$('.wpcf7-form-control-wrap').each(function() {
//console.log($(this));
$(this).on('focus', function() {
$(this).parent().parent().addClass('active');
});
$(this).on('blur', function() {
if ($(this).val().length == 0) {
$(this).parent().parent().removeClass('active');
}
});
if ($(this).val() != '') $(this).parent('.grid-3').addClass('active');
});
的HTML:
<div class="grid-3">
<label for="schenkel2">Schenkel 2 in mm</label>
<span class="wpcf7-form-control-wrap schenkel2">
<input type="text" name="schenkel2" value="" size="40" class="wpcf7-form-control wpcf7-text" id="schenkel2" aria-invalid="false">
</span>
</div>
为什么你需要添加焦点一类?它是否改变样式?如果是这样的话,':focus'伪类就是为这样的场合而制作的 –