未访问全局变量,代码未运行

时间:2016-03-17 14:04:04

标签: javascript

首先发帖,抱歉,如果我做错了。

var tierArray = [390, 520, 750, 975, 1350, 1820, 3250];
var rand = Math.floor((Math.random() * 6) + 1);
var rp;
var skinName = document.getElementById("skinName");
var skinRP = document.getElementById("skinRP");
var totalRP = document.getElementById("totalRP");
var totalMG = document.getElementById("totalMG");
var total = parseInt(totalRP.value);
var mg = parseInt(totalMG.value);

var array390, array520, array750, array975, array1350, array1820, array3250 = [];
var skinImg = document.getElementById("skin");
var activeArray;

function roll() {
    mg += 490;
    rp = tierArray[rand];
    total += rp;

    skinName.value = "hello";
    skinRP.value = rp;
    totalRP.value = total;
    totalMG.value = mg;
}

我的页面在将所有变量移动到全局范围之前工作,以便它们可用于其他方法,现在它们已被移动,代码无法运行,而且我的JSLint没有遇到问题,我是遗失了什么?

1 个答案:

答案 0 :(得分:0)

您运行代码时未加载函数外部的元素,这可能是因为您首先将脚本实现到页面,然后插入HTML DOM元素。

<head>
<script>         < The elements dont exist
<body>
<element>        < The elements exist
<script>         < Your script