开发者

Jquery focus() doesn't work when tabbing?

开发者 https://www.devze.com 2023-01-20 00:17 出处:网络
I have setup some basic code that triggers on focus of various input fields: var div = \'<div c开发者_Python百科lass=\"formHint\">Content</div>\';

I have setup some basic code that triggers on focus of various input fields:

        var div = '<div c开发者_Python百科lass="formHint">Content</div>';                 

       $j('.textbox').blur(function() {
          $j(".formHint").remove();
        });


       $j('.textbox').click(function() {

        $j(this).focus().before(div);
        $j(this).prev(".formHint").animate({ 
     marginLeft: "325px",
     opacity: "1"
     }, 200 );


});

Basically, it inserts a DIV after .textbox on focus(), and remove all .textbox on blur().

The problem is, when you tab through the elements, they don't gain focus. Jquerys documentation says

In recent browser versions, the event can be extended to include all element types by explicitly setting the element's tabindex property. An element can gain focus via keyboard commands, such as the Tab key, or by mouse clicks on the element.

But after doing some research, I don't understand how to apply this?


You need to handle the focus event, not the click event:

$('.textbox').focus(function() {
    $(this).before(div);
    $(this).prev(".formHint").animate({ 
        marginLeft: "325px",
        opacity: "1"
    }, 200 );
});
0

精彩评论

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