V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
sudoy
V2EX  ›  问与答

油猴脚本自动执行

  •  
  •   sudoy · Mar 22, 2022 · 1732 views
    This topic created in 1502 days ago, the information mentioned may be changed or developed.

    直接上油猴脚本代码:

    // ==UserScript==
    // @name         MyScript
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  try to take over the world!
    // @author       You
    // @match        https://mysite.com
    // @icon         https://www.google.com/s2/favicons?sz=64&domain=vuejs.org
    // @grant        none
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        // Your code here...
        function myFunc() {
            console.log('myFunc is called!');
        };
    
        var myButton = document.getElementById('myButton');
        myButton.addEventListener('click', myFunc());
    })();
    

    我想要的结果是:等用户点击 myButton 按钮以后才触发 myFunc

    现在的情况是:只要加载页面就会执行 myFunc

    求教各位老哥,该如何修改。我搜了一番下来没找到有帮助的文档。

    4 replies    2022-03-22 14:07:41 +08:00
    Trim21
        1
    Trim21  
       Mar 22, 2022 via Android   ❤️ 1
    myButton.addEventListener('click', myFunc);
    sudoy
        2
    sudoy  
    OP
       Mar 22, 2022
    @Trim21 原来是这里多了个括号,删掉问题就解决啦,感谢!
    zy445566
        3
    zy445566  
       Mar 22, 2022
    😂😂😂
    wanlei1766
        4
    wanlei1766  
       Mar 22, 2022
    本来想进来学习一番,没曾想一个括号就解决了 。😢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2128 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:33 · PVG 08:33 · LAX 17:33 · JFK 20:33
    ♥ Do have faith in what you're doing.