在JavaScript函数中定义全局变量是否可以在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") ? document.documentElement :
document.body;
}
function hidetrail() {
var x = document.getElementById("trailimageid").style;
x.visibility = "hidden";
document.onmousemove = "";
}
4 回答

婷婷同学_
TA贡献1844条经验 获得超8个赞
var
<script>var yourGlobalVariable;function foo() { // ...}</script>
window
:
<script>function foo() { window.yourGlobalVariable = ...;}</script>
var
window
let
, const
class
"use strict"
.)
window
window
id
name
name
<script>(function() { // Begin scoping function var yourGlobalVariable; // Global to your code, invisible outside the scoping function function foo() { // ... }})(); // End scoping function</script>

MYYA
TA贡献1868条经验 获得超4个赞
var trialImage;
function makeObj(address) { trialImage = [address, 50, 50];....}
添加回答
举报
0/150
提交
取消