开发者

how to change background position when focus on input

开发者 https://www.devze.com 2023-02-04 17:44 出处:网络
<div class=\"field\"> <input name=\"password\" type=\"password\" /> </div> I want to change the background image\'s position of the field div when there is focu开发者_运维百科s on
<div class="field">
    <input name="password" type="password" />
</div> 

I want to change the background image's position of the field div when there is focu开发者_运维百科s on input. Any idea to do it with jquery? Thanks.


I've not tried this, but I think the following should work:

$('input:password').focus(
    function(){
        $(this).parent().css({'background-position': 'Xpx Ypx'});
    }).blur(
    function(){
        $(this).parent().css({'background-position': 'Xpx Ypx'});
    });


You can watch for the focus event on your password input, and then change the background-position CSS property on target using jQuery's css() method.

$('div.field input[name="password"]').focus(function() {
    $(this).closest('div.field').css('background-position', '10px 10px');
});


$('.field input').focus(function() {
    $(this).addClass('focus')
});

Then just set the .field.focus classes background position.

0

精彩评论

暂无评论...
验证码 换一张
取 消