<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src='./vue.js'></script>
</head>
<body>
    <div id="app">
        <button id="hello" @click="clickHandleHello">Hello</button>
        <button id="world" @click="clickHandleWorld">World</button>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            methods: {
                clickHandleHello: function() {
                    alert("hello clicked")
                    setTimeout(function() {
                        debugger
                        var e = document.createEvent('MouseEvents')
                        e.initEvent('click', true, true)
                        document.getElementById('world').dispatchEvent(e)

                    }, 2000)
                },
                clickHandleWorld: function () {
                    alert("world clicked")
                }
            }
        })
    </script>
</body>
</html>

说明: 

  • 界面上有两个按钮:hello、world
  • 点击hello按钮将触发clickHandleHello()被调用
  • clickHandleHello()开启了一个2秒定时器,2秒后模拟一个点击事件并把它派发给world这个按钮
  • 最终会触发clickHandleWorld()被调用并弹出"world clicked"警告框
Logo

前往低代码交流专区

更多推荐