I have a form with a DIV, 3 INPUTS, each INPUT sits within a LABEL element. I would like to change the background image of the DIV element when focusing on each INPUT.
我有一个带有DIV,3个INPUTS的表单,每个INPUT都位于LABEL元素中。我想在聚焦每个INPUT时改变DIV元素的背景图像。
I can't move back up the DOM to fix this with CSS, so could someone suggest a few lines of jQuery please?
我无法通过CSS移回DOM修复此问题,所以有人可以建议几行jQuery吗?
Thanks
3 个解决方案
#1
3
$('div input').focus(function(){
$(this).parents('div:eq(0)').addClass('specialCSSclass');
}).blur(function(){
$(this).parents('div:eq(0)').removeClass('specialCSSclass');
});
You would need to create a class in your CSS and then replace "specialCSSclass" with it.
您需要在CSS中创建一个类,然后用它替换“specialCSSclass”。
#2
1
$('input').focus(function(){
$(this).parent().parent().addClass('highlight');
}).blur(function(){
$(this).parent().parent().removeClass('highlight');
});
#3
0
jQuery closest is also an option.
jQuery最近也是一个选项。
closest( selector )
.closest( selector )
.closest( selector, [ context ] )
closest( selectors, [ context ] )
.closest( selectors, [ context ] )
Per the description.. Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree.
根据描述..获取与选择器匹配的第一个祖先元素,从当前元素开始并逐步向上遍历DOM树。
#1
3
$('div input').focus(function(){
$(this).parents('div:eq(0)').addClass('specialCSSclass');
}).blur(function(){
$(this).parents('div:eq(0)').removeClass('specialCSSclass');
});
You would need to create a class in your CSS and then replace "specialCSSclass" with it.
您需要在CSS中创建一个类,然后用它替换“specialCSSclass”。
#2
1
$('input').focus(function(){
$(this).parent().parent().addClass('highlight');
}).blur(function(){
$(this).parent().parent().removeClass('highlight');
});
#3
0
jQuery closest is also an option.
jQuery最近也是一个选项。
closest( selector )
.closest( selector )
.closest( selector, [ context ] )
closest( selectors, [ context ] )
.closest( selectors, [ context ] )
Per the description.. Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree.
根据描述..获取与选择器匹配的第一个祖先元素,从当前元素开始并逐步向上遍历DOM树。