是否可以使用JS或jQuery为DIV编写onFocus / lostFocus处理程序? [英] Is it possible to write onFocus/lostFocus handler for a DIV using JS or jQuery?

查看:164
本文介绍了是否可以使用JS或jQuery为DIV编写onFocus / lostFocus处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个div,当用户点击div时,应该调用一个函数。当用户点击其他东西(除了这个div之外的任何东西)时,应该调用另一个函数。

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 called.

所以基本上我需要有onFocus()和lostFocus()函数调用与此DIV相关联。它是用JavaScript还是jQuery提供的?

So basically i need to have onFocus() and lostFocus() function calls associated with this DIV. Is it available in JavaScript or even in jQuery?

谢谢。

推荐答案

在这里,我创建了一个你想要的工作演示 http://jsfiddle.net/kPbfL/1/

Here i created a Working demo of what you want http://jsfiddle.net/kPbfL/1/

您需要将 tabindex 属性添加到 div

加价:

 <div id="mydiv" tabindex="-1"></div>

Javascript

$("#mydiv").focusin(function() {
  $("#mydiv").css("background","red");
});
$("#mydiv").focusout(function() {
  $("#mydiv").css("background","white");
});

CSS

#mydiv{
width : 50px;
    height:50px;
    border : 1px solid red;
}​
​

这篇关于是否可以使用JS或jQuery为DIV编写onFocus / lostFocus处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆