通过JavaScript中的函数传递变量

时间:2014-10-23 21:46:39

标签: javascript

我试图将它放在哪里,如果一个函数运行并定义变量,另一个函数将能够重新组合该函数。我对Javascript有点新意,所以我的代码可能只是一堆乱七八糟。

function next() {
    var random = Math.floor(Math.random() * 15)
    document.getElementById("image").src = picture[random];
    console.log(random);
}

function who() {
    var thing = things[random];
    var text = document.getElementById("am").innerHTML = "I am " + thing + ".";
    console.log(text);
}

2 个答案:

答案 0 :(得分:2)

像这样:

var random;

function next() {
    random = Math.floor(Math.random() * 15);
    document.getElementById("image").src = picture[random];
    console.log(random);
}

function who() {
    var thing = things[random];
    var text = document.getElementById("am").innerHTML = "I am " + thing + ".";
    console.log(text);
}

答案 1 :(得分:0)

首先定义随机变量,然后将其发送到函数,就像这样;

var random = Math.floor(Math.random() * 15);

next(random);
who(random);

function next(random) {

    document.getElementById("image").src = picture[random];
    console.log(random);
}

function who(random) {
    var thing = things[random];
    var text = document.getElementById("am").innerHTML = "I am " + thing + ".";
    console.log(text);
}