JavaScript 函数调用
javascriptweb developmentfront end technology更新于 2024/7/13 10:50:00
函数调用用于在定义函数后,在函数名称后添加 () 来执行函数定义中花括号内的代码,从而调用特定函数。
以下是实现 JavaScript 函数调用的代码 −
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Function Invocation</h1> <div class="sample"></div> <button class="btn">点击此处</button> <h3> Click on the above buttons to invoke the function </h3> <script> let sampleEle = document.querySelector(".sample"); function helloWorld() { alert("HELLO WORLD"); } document.querySelector(".Btn").addEventListener("click", () => { helloWorld(); }); </script> </body> </html>
输出
点击“CLICK HERE”按钮 −