搜尋

首頁  >  問答  >  主體

Switch 語句:展示影像指南

我正在製作一個質量效應人格測驗,該測驗改編自 90 年代的 EasyDamus D&D 測驗,但我在使用結果頁面時遇到了問題。也就是說,我試圖包含一個圖像來匹配每個結果,但當程式碼使用 switch 語句實現結果時,我不知道該怎麼做。這是程式碼片段:

var win = window.open("", "win","width=900,height=550,top=25,left=50,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes");

with (win.document) {
    open("text/html", "replace");
        
    write("<html><head><link rel='stylesheet' type='text/css' href='index.css'><title>Your Results Are In!\<\/title>\<\/head>");
            write("<body text='#FFFFFF' font-family='massEffect' link='#5555FF' vlink='#6666EE' bgcolor='#F2ECDA' background='images/space.png'>");

    write("<center><h2><b><font color='#FFFFFF'>You Are A:\<\/font>\<\/b>\<\/h2>\<\/center>");
    write("<br><center><h1><b>");
            
    
    switch (race) {
        case "human": write("Human\<\/b> "); break;
        case "asari": write("Asari\<\/b> "); break;
        case "turian": write("Turian\<\/b> "); break;
        case "salarian": write("Salarian\<\/b> "); break;
        case "krogan": write("Krogan\<\/b> "); break;
        case "quarian": write("Quarian\<\/b> "); break;
        case "geth": write("Geth\<\/b> "); break;
        case "volus": write("Volus\<\/b> "); break;
        case "rachni": write("Rachni\<\/b> "); break;
        case "batarian": write("Batarian\<\/b> "); }

    switch (primclass) {
        case "soldier": write(" Soldier"); break;
        case "infiltrator": write(" Infiltrator"); break;
        case "engineer": write(" Engineer"); break;
        case "adept": write(" Adept"); break;
        case "sentinel": write(" Sentinel"); break;
        case "vanguard": write(" Vanguard"); }

    switch (secclass) {
        case "soldier": write("/Soldier"); break;
        case "infiltrator": write("/infiltrator"); break;
        case "engineer": write("/Engineer"); break;
        case "adept": write("/Adept"); break;
        case "sentinel": write("/Sentinel"); break;
        case "none": write(""); break;
        case "vanguard": write("/Vanguard"); }



write("<br><h2><br>Race:<br></h2>");

    switch (race) {
    case "human": `

最後一行是每個結果的風味文本,雖然它的顯示方式沒有問題,但我正在嘗試找到一種在其前面放置圖像的方法。我有可用的圖片,其中大部分都是從本機資料夾呼叫的陣列。讓影像出現的最佳方式是什麼?

我已經搜尋了幾週的解決方案,其中大多數都推薦事件偵聽器,但我一直無法讓它為我工作。

P粉557957970P粉557957970327 天前468

全部回覆(1)我來回復

  • P粉413704245

    P粉4137042452024-01-30 11:27:51

    這段程式碼做了很多事。首先,對於 switch 語句,它應該為特定情況設定一些變數。另外,為每個 switch 語句提供 default 案例總是一個好習慣。

    function decideOnRace(race) {
        let result = '';
        switch (race) {
            case "human":
                result = "Human\<\/b> ";
                break;
            default:
                result = 'something else";
                break;
        }
    }
    
    // place this accordingly
    const raceTextToWrite = decideOnRace(race);
    write(raceTextToWrite);

    在所有不同的情況下遵循這種做法應該會幫助你進步,例如,對於 secclass、primclass 等。

    回覆
    0
  • 取消回覆