簡體   English   中英

通過JavaScript中的函數傳遞變量

[英]Passing Variables through Functions in JavaScript

我試圖使它在其中一個函數運行並定義變量的位置,而另一個函數將能夠協調該函數。 我是Java的新手,所以我的代碼可能只是一堆雜物。

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);
}

像這樣:

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);
}

首先定義隨機變量,然后將其發送給函數,如下所示;

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM