是否可以在JavaScript函数中定义全局变量?
我想在其他函数中使用trailimage变量(在makeObj函数中声明).
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
var offsetfrommouse = [10, -20];
var displayduration = 0;
var obj_selected = 0;
function makeObj(address) {
**var trailimage = [address, 50, 50];**
document.write('<img id="trailimageid" src="' + trailimage[0] + '" border="0" style=" position: absolute; visibility:visible; left: 0px; top: 0px; width: ' + trailimage[1] + 'px; height: ' + trailimage[2] + 'px">');
obj_selected = 1;
}
function truebody() {
return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? …Run Code Online (Sandbox Code Playgroud) 我正在测试ES6模块,并希望让用户使用onclick以下方法访问一些导入的函数:
的test.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Module Test</title>
</head>
<body>
<input type="button" value="click me" onclick="hello();"/>
<script type="module">import {hello} from "./test.js";</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
test.js:
export function hello() {console.log("hello");}
Run Code Online (Sandbox Code Playgroud)
当我单击按钮时,开发人员控制台会说:ReferenceError:未定义hello.如何从模块导入函数,以便它们可用作onclick函数?
我正在使用Firefox 54.0 dom.moduleScripts.enabled设置为true.
您好,我是 js 新手,我试图在导入 js 模块时了解本地/全局范围。
下面是我正在运行的 html/js 文件。

下面是我的 html/js 文件,它不起作用。
请告诉我为什么我必须放置 document.querySelector~~~ 以及为什么第二个不起作用..
javascript ×3
scope ×2
declaration ×1
ecmascript-6 ×1
es6-modules ×1
function ×1
module ×1
variables ×1