您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

bootstrap双击事件怎么写

2025/5/29 7:38:10发布25次查看
推荐教程:bootstrap教程
bootstrap-treeview是一款效果非常酷的基于bootstrap的jquery多级列表树插件。该jquery插件基于twitter bootstrap,以简单和优雅的方式来显示一些继承树结构,如视图树、列表树等等。但是不知为什么这个插件没有自带双击事件。
这个双击事件的解决方案中使用到了bootstrap-treeview自带的两个事件nodeselected和nodeunselected.如果在treeview的节点上双击一定会触发选中事件和取消选中事件,计算这两个时间的时间间隔就可以模拟出双击事件的效果了。双击事件每次点击鼠标左键的间隔,人为操作300毫秒足够。
代码如下:
<!doctype html><html> <head> <meta charset="utf-8" /> <title></title> <link href="css/bootstrap.css" rel="stylesheet" /> </head> <body> <div id="tree" style="width: 400px;height: 1000px;margin-left: auto;margin-right: auto;"></div> <div id="testdate"></div> <script src="js/jquery.js"></script> <script src="js/bootstrap-treeview.js"></script> <script type="text/javascript"> //获取树形结构列表数据 function gettree() { var tree = [{ text: "parent 1", nodes: [{ text: "child 1", nodes: [{ text: "grandchild 1" }, { text: "grandchild 2" }] }, { text: "child 2" }] }, { text: "parent 2" }, { text: "parent 3" }, { text: "parent 4" }, { text: "parent 5" }]; return tree; } //初始化树形结构列表 $('#tree').treeview({ data: gettree() }); //最后一次触发节点id var lastselectednodeid = null; //最后一次触发时间 var lastselecttime = null; //自定义业务方法 function custombusiness(data){ alert("双击获得节点名字: "+data.text); } function clicknode(event, data) { if (lastselectednodeid && lastselecttime) { var time = new date().gettime(); var t = time - lastselecttime; if (lastselectednodeid == data.nodeid && t < 300) { custombusiness(data); } } lastselectednodeid = data.nodeid; lastselecttime = new date().gettime(); } //自定义双击事件 function customdblclickfun(){ //节点选中时触发 $('#tree').on('nodeselected', function(event, data) { clicknode(event, data) }); //节点取消选中时触发 $('#tree').on('nodeunselected', function(event, data) { clicknode(event, data) }); } $(document).ready(function() { customdblclickfun(); }); </script> </body></html>
效果图:
重点:
最主要的全局变量:
lastselectednodeid,lastselectednodeid
最主要的方法:
clicknode()
上面这个方法主要用来判断选中事件和取消选中事件操作的目标是否是一个且时间间隔是否足够小。符合这两个条件客户就是想触发双击事件。可以再函数custombusiness中自定义业务逻辑。
原文章地址:https://www.cnblogs.com/chengxuyuanzhilu/p/5114155.html
以上就是bootstrap双击事件怎么写的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product