这章与上一张《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. Spring学习笔记

    Spring 的控制反转:把对象的创建.初始化.销毁等工作交给Spring 容器来做,有spring容器控制对象的生命周期 applicationContext.xml beans --->sp ...

  2. SharePoint Calculated Columns 分类: Sharepoint 2015-07-09 01:49 8人阅读 评论(0) 收藏

    SharePoint Calculated Columns are powerful tools when creating out-of-the-box solutions. With these ...

  3. JS技术大全

    事件源对象:event.srcElement.tagName  event.srcElement.type 捕获/释放:event.srcElement.setCapture();  event.sr ...

  4. 第十篇.bootstrap轮播

    使用bootstrap的轮播插件可以向站点添加滑块,内容可以是图像,内嵌框架,视频或其它任何内容,使用轮播插件需要引入bootstrap.min.js. <div id="carous ...

  5. Oracle存储过程例子:运用了正则表达式、数组等

    代码 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- ...

  6. centos上libreoffice+unoconv安装步骤,实现word转pdf

    一.libreoffice安装 1.yum search  libreoffice查询一下系统自带的安装包 安装libreoffice.x86_64这个就可以了   2.yum install lib ...

  7. marquee 实现首尾相连循环滚动效果

    <marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标签不仅可以滚动文字,也可以滚动图片,表格等  marquee标签不是HTML3.2 ...

  8. SparkMLlib之 logistic regression源码分析

    最近在研究机器学习,使用的工具是spark,本文是针对spar最新的源码Spark1.6.0的MLlib中的logistic regression, linear regression进行源码分析,其 ...

  9. C# winform 安装程序打包(自定义操作)

    (一),安装程序 以前用vs制作过安装程序,现在把步骤写出来,有帮助的大家一定要顶哦 第一步:建立工程1.打开vs,新建项目->其他项目类型->安装和部署(這個子项下面有安装项目和Web安 ...

  10. 团队开发——冲刺2.e

    冲刺阶段二(第五天) 1.昨天做了什么? 讨论“暂停”功能:编写软件测试计划书(引言.测试内容). 2.今天准备做什么? A.编写软件使用说明书: B.编写软件测试计划书(测试规则.测试环境): C. ...