简体   繁体   English

如何让 html 按钮与 java 脚本函数一起使用?

[英]how to I get a html button to work with a java script function?

I have a button in a html file with that when clicked should run a function in a java script file that send an alert that says hey.我在 html 文件中有一个按钮,单击该按钮时应该在 java 脚本文件中运行一个函数,该函数会发送一个警报,说嘿。 This doesn't happen and I don' know why.这不会发生,我不知道为什么。 It feels like the simplest thin but I just can't get it to work.感觉就像最简单的瘦,但我就是无法让它发挥作用。 Java Script is enabled in my browser (google chrome) I don't think it matters but just in case, I'm codeing in VS code.在我的浏览器(谷歌浏览器)中启用了 Java Script 我认为这并不重要,但以防万一,我在 VS 代码中进行编码。

 function myfunction() { alert("hey"); }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="script.js" type="module"></script> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <div id="head"> <h1>Park Stats</h1> </div> <table> <tr id="head_row"> <th>Player</th> <th>Points</th> <th>Rebounds</th> <th>Blocks</th> <th>Steals</th> <th>Fouls</th> <th>Scrum Wins</th> <th>Scrum Losses</th> <th>Assists</th> </tr> <tr> <td>Player Name<button class="name_button">✎</button></td> <td> <div class="container"> <button>-</button> <div id="p1s1" class="stat">0</div> **<button onclick="myfunction();">+</button>** </div> </td> </tr> </table> </body> </html>

If you are using your code local and not on a webserver you have to remove the attribute type="module" .如果您在本地而不是在网络服务器上使用代码,则必须删除属性type="module" see: javascript modules and CORS请参阅: javascript 模块和 CORS

and MDN JavaScript modulesMDN JavaScript 模块

Change this改变这个

<script src="script.js" type="module"></script>

To

<script src="script.js"></script>

And now it works for me.现在它对我有用。

**<button onclick="myfunction();">+</button>**应该是**<button id="alert-button">+</button>**

document.addEventListener('DOMContentLoaded', (event) => { document.getElementById("alert-button").addEventListener('click', function(){ myFunction(); }) });

暂无
暂无

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

相关问题 Java 脚本功能不起作用,如何将我的 javascript 文件添加到 html 并使其工作 - Java Script Functions Don't work and How do i add my javascript file to html and get it to work 如何在Java脚本的弹出窗口中运行并获取html提交按钮的值 - how to run and get the value of html submit button on popup in java script 如何从放置在 php 脚本中的 html 按钮调用 Java Script 函数? - How to call a Java Script function from a html button placed inside a php script? Javascript:如何在此脚本中使split()函数起作用 - Javascript: How do I get the split() function in this script to work 如何在页面加载时调用 function 以使用按钮 - How do I get function called on page load to work with button java脚本中的点击按钮如何控制这个function - How to control this function with the click button in java script 如何在HTML按钮的“ onClick”上调用Java函数? - How to call java function on “onClick” of html button? 如何从 java 脚本文件中调用 function 在另一个 function 内? - How do I call a function inside of another function from java script file in html file? 如何更改此html和脚本以使用6个不同的按钮与6个单选和1个按钮一起使用 - How to change this html and script to work with 6 different buttons vs 6 radio & 1 button 单击素面中的命令按钮时如何调用Java脚本函数 - How do i invoke a java script function when clicking on command button in prime faces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM