我對JavaScript比較「新」,想知道在功能中加入if
是否需要添加任何內容。這是我的代碼。我希望能夠使用一個函數來調用另一個提醒頁面的函數。Javascript:當在功能中添加if時,是否需要添加任何內容?
function Function() {
var var1 = Math.floor((Math.random() * 10) + 1);
document.getElementById("paragraph1").innerHTML = var1;
Functions();
}
function Functions() {
if (var1 == 1) {
Question1();
}
}
function Question1() {
alert("works");
}
感謝高級!
您需要了解一些關於變量範圍的內容。 'Function'中的'var1'不能從'Functions'中獲得。這將是如果JS有動態範圍,但事實並非如此。 – 2017-02-24 22:59:47
不,但'函數'不能訪問'var1'。您必須將其作爲來自'Function'的參數傳遞。 – adrice727
除非是構造函數,否則它也習慣於不使funcs大寫 – pixelearth