簡體   English   中英

Javascript函數未打印到控制台上

[英]Javascript function not printing onto the console

為什么以下內容未打印到控制台? 我不明白錯誤是什么..可以有人幫我調試錯誤。

<script type="text/javascript" src="static/js/scenes/scene_world.js">
  //document.write("<button type='button' onclick='click();'>Click Me!</button>");
  //document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");
    console.log("lol");
  </script>

在scene_world.js中:

  function lol(){
  console.log("lol");
  }

像這樣從外部訪問它很累:

  <script type="text/javascript" src="static/js/scenes/scene_world.js">
  //document.write("<button type='button' onclick='click();'>Click Me!</button>");
  //document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");

  </script>
  <script>
  document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");
  </script>

但這給了我這個錯誤:

未捕獲的ReferenceError:未定義大聲笑

具有src屬性的script標簽也不能具有內聯內容。 您需要為此創建一個新的腳本塊。

根據官方的 html規范:

如果src具有URI值,則用戶代理必須忽略元素的內容,並通過URI檢索腳本

有關詳細信息,請參見參考

w3c開始

如果未設置src屬性,則用戶代理必須將元素的內容解釋為腳本。 如果src具有URI值,則用戶代理必須忽略元素的內容,並通過URI檢索腳本。

您應該使用src屬性清除<script>標記之間的所有內容,並將代碼放在單獨的<script>標記中。

<script type="text/javascript" src="static/js/scenes/scene_world.js"></script>
<script>
document.write("<input id='clickMe' type='button' value='clickme' onclick='lol();' />");
</script>

參見DEMO

好的,編輯后, scene_world.js很好。 為什么要使用document.write(..)

<script type="text/javascript" src="static/js/scenes/scene_world.js">
</script>
<input id='clickMe' type='button' value='clickme' onclick='lol();'/>

單擊按鈕時應該可以使用。

直接致電:

<script type="text/javascript" src="static/js/scenes/scene_world.js">
</script> 
<script type="text/javascript">
    lol();
</script>

您是否已將這些腳本標簽插入headbody內部?

它沒有打印到控制台上,因為我嘗試調用的功能在另一個功能內。 因此未正確調用它。 一個粗心的錯誤。

暫無
暫無

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

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