Я не верю, что элемент form
может вызвать событие focus
, поэтому вы можете установить цвет на основе событий blur
и focus
всех элементов в форме.
Markup:
<form id="myForm" method="post">
<input type="text" />
<input type="text" />
</form>
JQuery:
//receives focus
$("input, select").focus(function () {
$("#myForm").css("background-color", "#fff");
//OR
//to swap a class with the CSS defined instead do this
$("#myForm").addClass("your-class-name");
});
//loses focus
$("input, select").blur(function () {
$("#myForm").css("background-color", "#000");
//OR
//to swap a class with the CSS defined instead do this
$("#myForm").removeClass("your-class-name");
});
селектор input, select
выберет все входы и выпадающие списки на странице. Вы можете дополнительно ограничить его, добавив префикс формы также, если это необходимо.