Is it possible to write onFocus/lostFocus handler for a DIV using JS or jQuery?

前端 未结 4 1204
滥情空心
滥情空心 2020-12-10 05:55

I have a div and when the user clicks the div a function should be called. And when the user clicks something else (anything other than this div) another function should be

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 06:24

    You need to add tabindex attribute to div :

    $("#mydiv").focusin(function() {
      $("#mydiv").css("background", "red");
    });
    $("#mydiv").focusout(function() {
      $("#mydiv").css("background", "white");
    });
    #mydiv {
      width: 50px;
      height: 50px;
      border: 1px solid red;
    }
    
    

提交回复
热议问题