我渴望解決我的問題與自動生成輸入跨度。我在我的網站上使用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'僞類就是爲這樣的場合而製作的 –