繁体   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