我正在研究石頭、紙、剪刀頁。我制作了一個結果頁面,該頁面在計算機展示它選擇的內容后加載。我想要的是基于結果,結果頁面中有兩個影像發生變化。我制作了具有我想要更改的屬性的函式,但是一旦加載結果頁面,我無法弄清楚如何使頁面運行這些函式。我是 Javascript 的超級新手,所以任何幫助將不勝感激!
const getResult = () => {
computerChoice = rand(0, 3);
finalChoice = imgs[computerChoice];
computer.src = `img/${finalChoice}.png`
computerButton.innerHTML = finalChoice;
console.log(computer.src);
// console.log(imgs.indexOf(finalChoice));
computerIndex = imgs.indexOf(finalChoice)
userChoice = 1;
if (userChoice === computerIndex) {
console.log("is a tied");
setTimeout("resultPage()", 1000);
} else if (userChoice < computerIndex) {
console.log("you win");
setTimeout("resultPage()", 1000);
} else if (userChoice > computerIndex) {
console.log("you lost")
setTimeout("resultPage()", 1000);
}
}
const resultPage = () => {
window.location = "finalResult.html";
}
function loseAttributes() {
let weaponImg = document.querySelector(".finalweapon");
let resultLabel = document.querySelector(".result");
weaponImg.src = "img/weapons-10.png";
resultLabel.src = "img/label2.png";
}
function tieAttributes() {
let weaponImg = document.querySelector(".finalweapon");
weaponImg.src = "img/Scissors.png";
let resultLabel2 = document.querySelector(".result");
resultLabel2.src = "img/label3.png";
}
uj5u.com熱心網友回復:
將引數傳遞給 html 頁面的一種方法是使用 url。因此,當贏得重定向到finalResult.html#winif lost thenfinalResult.html#lose等時,然后在 finalResult.html 的加載事件中使用這些引數window.location.hash
// finalResult.html
window.addEventListener('load', function() {
var hash = window.location.hash
if (hash === "#win") {
// show win image
}
if (hash === "#lose") {
// show lose image
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/517959.html
上一篇:如何在SearchWord方法C#中回傳帶有訊息的布林值?
下一篇:幾個if條件?
