Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我创建了一个包含多个 Javascript 脚本的 HTML 文档。我在页面的头部创建了一个生成特定值的 Javascript 函数。我想在位于正文部分的 Javascript 块中传递这个变量。可能吗?谢谢!
对的,这是可能的。只需将头部中的函数设为全局,然后从正文脚本中调用它。或者在头脚本中调用它并将结果分配给您在下面访问的全局变量。
你的问题不是很具体。但这完全符合您的描述并且有效。
<html> <head> <script>var test = 'someVar';</script> </head> <body> <script> alert (test); </script> </body>