繁体   English   中英

Webflow中点击Button时如何执行JavaScript代码

[英]How to execute JavaScript code when click Button in Webflow

我是 Webflow 的新手,在 Webflow 项目中,我想通过 id 获取按钮并在单击此按钮时执行一些 Javascript 代码。

我写的代码:

<script>
    document.addEventListener('DOMContentLoaded', () => {
       let btn = document.getElementById('myButtonId');
       btn.addEventListener('click', () => {
           // handle the click event
           console.log('clicked');
           alert("Hello");
       });
    });
</script>

您要么需要将代码移动到页脚,

或将代码包装在 Webflow 推荐的“Webflow 就绪功能”中:

var Webflow = Webflow || {};
Webflow.push(function() {

    // Your code goes here
    document.addEventListener('DOMContentLoaded', () => {
       let btn = document.getElementById('myButtonId');
       btn.addEventListener('click', () => {
          ...
       });
    });

}); // End Webflow ready function

Java 脚本你可以调用 function 任何你想要的名字

 <script>
            function yourFunctionName() {
            alert("hello")
            };
        
        </script>

HTML

<body>
    <button id="myButtonId" onclick="yourFunctionName ()">click here</button>
 
</body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM