这章与上一张《jquery input 下拉框(模拟select控件)焦点事件》类似

这章讲述div的焦点事件如何使用

div的焦点事件与input的焦点事件区别在于 需要多添加一个属性:tabindex (Safari可能不支持) ; 这个属性是可以让键盘获取到焦点事件,当然,我们只是用这个属性来让div有焦点而已; 为了不改变网页原有的键盘属性; 建议设置成  tabindex = '-1';  tabindex 默认为0,即在网页中按下tab即可触发,第一下tab就触发当前事件;

ps : div还有一个属性 (html5新属性) 可以让其获得焦点,但不建议用在这; contenteditable="true"; 这个属性还有一个特性就是可以让div变成input那样,可编辑特性!

下面是html结构:

  <div class="div-box" tabindex="-1">
<p class="text"><span class="texts">Holle Word</span> <span class="tip"></span></p>
<ul class="dropdown-menu">
<h4>Holle Word</h4>
<hr>
<li>test</li>
<li>dome</li>
<li>标签</li>
</ul>
</div>

css :

    .div-box {
position: relative;
display: inline-block;
outline:;
} .text {
height: 32px;
line-height: 32px;
margin:;
padding: 0 6px;
cursor: pointer;
} .dropdown-menu {
position: absolute;
width: 200px;
color: #FFF;
margin:;
padding: 10px;
list-style: none;
border-radius: 10px;
background-color: #555;
-webkit-box-shadow: 0 5px 15px rgba(0, 0, 0, .5);
box-shadow: 0 5px 15px rgba(255, 255, 255, .1);
} .dropdown-menu h4 {
text-align: center;
margin: 10px 0;
} .dropdown-menu li {
border-bottom: 1px solid #999;
padding: 0 6px;
line-height: 28px;
} .dropdown-menu li:hover {
background-color: black;
cursor: pointer;
} .tip {
width:;
height:;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 10px solid #555;
display: inline-block;
}

js :

    var isBox = false; // 定义一个触发焦点事件的开关,默认为不开启状态 || 也可以给input设置一个属性,来判断
$(".dropdown-menu").hide();
$(".div-box").focus(function () { // div绑定焦点事件,触发时打开焦点开关
$(this).find(".dropdown-menu").show();
isBox = true;
})
$(".div-box").mousemove(function () { // 鼠标在div区域内打开焦点开关
isBox = true;
});
$(".div-box").mouseout(function () { // 鼠标在div区域外关闭焦点开关
isBox = false;
});
$(".div-box").blur(function () { // div失去焦点时通过焦点开关状态判断鼠标所在区域
if (isBox == true) return false;
$(this).find(".dropdown-menu").hide();
});
$(".dropdown-menu").find('li').each(function () {
$(this).on("click", function () {
isBox = false;
var text = $(this).text();
$(this).parent().siblings(".text").find(".texts").text(text);
$(this).parent().parent().blur(); // ********* 清除焦点事件 (div本身是没焦点的)
$(this).parent().hide();
})
})

这章与上一章《jquery input 下拉框(模拟select控件)焦点事件》大致相同 ,区别在于tabindex 属性 和 在点击后需要移除div的焦点事件;

本章链接:http://www.cnblogs.com/ZevEssay/p/5953205.html

jquery div 下拉框焦点事件的更多相关文章

  1. jQuery对下拉框Select操作总结

    jQuery对下拉框Select操作总结 转自网络,留做备用 jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change( ...

  2. jQ给下拉框绑定事件,为什么要绑定在框(select标签)上,而不是绑定在选项(option标签)上

    这是我在学习锋利的 jquery 书中 5.1.4 的代码时遇到的一个小问题,源代码如下: <head> <style type="text/css"> * ...

  3. select change下拉框改变事件 设置选定项,禁用select

    select change下拉框改变事件 设置选定项,禁用select 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...

  4. jQuery的下拉框应用

    jQuery的下拉框应用 jQuery的下拉框左右选择应用 直接上代码 <!DOCTYPE html> <html> <head> <meta charset ...

  5. Jquery操作下拉框(DropDownList)实现取值赋值

    Jquery操作下拉框(DropDownList)想必大家都有所接触吧,下面与大家分享下对DropDownList进行取值赋值的实现代码 1. 获取选中项: 获取选中项的Value值: $('sele ...

  6. jquery 获取下拉框值与select text

    下面先介绍了很多jquery获取select属性的方法,同时后面的实例我们讲的是jquery 获取下拉框值与select text代码. 下面先介绍了很多jquery获取select属性的方法,同时后 ...

  7. js,jquery获取下拉框选中的option

    js获取select选中的值: var sel=document.getElementById("select1"); var index = sel.selectedIndex; ...

  8. Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢)

    Jquery操作下拉框(DropDownList)的取值赋值实现代码(王欢) 1. 获取选中项: 获取选中项的Value值: $('select#sel option:selected').val() ...

  9. jQuery操作下拉框的text值和val值

    jQuery操作下拉框的text值和val值 1,JS源码 <select name="select1" id="select1" style=" ...

随机推荐

  1. Bootstrap<基础二十四> 缩略图

    Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...

  2. html页面清除缓存

    需求:页面每次打开时清除本页面的缓存. 页面打开时,由于缓存的存在,刚刚更新的数据有时无法在页面得到刷新,当这个页面作为模式窗口被打开时问题更为明显. 解决办法为: (1) 用HTML标签设置HTTP ...

  3. 百度echarts地图扩展动态加载geoCoord

    var data={}; for(var i=0;i<result.length;i++){ data[(""+result[i].name+"")]=e ...

  4. iOS程序启动过程

    First, the function creates the main application object (step 3 in the flowchart). If you specify ni ...

  5. CALayer anchorPoint 锚点始终为(0,0)

    objc.io 学习 摘自原处修改 对层的属性详细了解可见这里 @interface ClockFace : CAShapeLayer@property (nonatomic, strong) NSD ...

  6. android之fragment

    觉得写得好:http://blog.csdn.net/shulianghan/article/details/38064191

  7. 5、 Android 之Fragment

    上:http://blog.csdn.net/lmj623565791/article/details/37970961 下:http://blog.csdn.net/lmj623565791/art ...

  8. Fiddler-1 安装

    1 进入Fiddler官网:http://www.telerik.com/fiddler 点击[Free download]:填写一些信息后就可以下载. 2 双击安装包--下一步dinghanhua下 ...

  9. Intent之前的对象传递与fragment传递数据

    Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...

  10. LeetCode Restore IP Addresses

    DFS class Solution { public: vector<string> restoreIpAddresses(string s) { return insertDot(s, ...