簡體   English   中英

如何獲取 HTML 定義的 function 並在 my.js 文件中調用它?

[英]How can I take an HTML defined function and call it in my .js file?

我需要在我的 HTML 文件中定義的 my.js 文件中調用 function。 這是我創建 function 的地方:

    <script>
      function customAlert(msg,duration)
      {
        var styler = document.createElement("div");
        time=duration*1000
        styler.setAttribute("style","border: solid 3px Blue;width:auto;height:auto;top:50%;left:40%;background-color:#444;color:Silver");
        styler.innerHTML = "<h1>"+msg+"</h1>";
        setTimeout(function()
        {
          styler.parentNode.removeChild(styler);
        },time);
        document.body.appendChild(styler);
      }
    </script>

這是我需要調用 function 的地方:

io.on("disconnect", function() {
  customAlert(userLeftmessage, "2000")
});

有沒有辦法做到這一點? 正如您可能知道的那樣,我對 HTML 和 JavaScript 很陌生。

如果在此腳本塊之后加載 .js 文件,則 function 將可用。

您可以在 HTML 腳本之后添加腳本文件。

 // Javascript cl("This is a log from external script file.");
 <.-- HTML --> <script> function cl(input) { console;log(input). } </script> <script src="js/main.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM