bootstrap双击事件怎么写


本文摘自PHP中文网,作者angryTom,侵删。

推荐教程:Bootstrap教程

  bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件。该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一些继承树结构,如视图树、列表树等等。但是不知为什么这个插件没有自带双击事件。

  这个双击事件的解决方案中使用到了bootstrap-treeview自带的两个事件"nodeSelected"和"nodeUnselected".如果在treeview的节点上双击一定会触发选中事件和取消选中事件,计算这两个时间的时间间隔就可以模拟出双击事件的效果了。双击事件每次点击鼠标左键的间隔,人为操作300毫秒足够。

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

<!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>

效果图:

B21.png

重点:

  最主要的全局变量:

    lastSelectedNodeIdlastSelectedNodeId

  最主要的方法:

    clickNode()

  上面这个方法主要用来判断选中事件和取消选中事件操作的目标是否是一个且时间间隔是否足够小。符合这两个条件客户就是想触发双击事件。可以再函数customBusiness中自定义业务逻辑。

原文章地址:https://www.cnblogs.com/chengxuyuanzhilu/p/5114155.html

以上就是bootstrap双击事件怎么写的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

详解bootstrap中的辅助类

bootstrap前端视图中如何实现页面内容模块化的隔离

怎么解决bootstrap乱码问题

bootstrap开源么

聊聊jsp页面中导入bootstrap的方法

bootstrap模态框有什么用

bootstrap可以更改样式吗

react和bootstrap区别

bootstrap做什么用的?

详解bootstrap中的缩略图

更多相关阅读请进入《bootstrap》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...