亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

函數無法使用從地址欄解析的字符串作為參數

函數無法使用從地址欄解析的字符串作為參數

神不在的星期二 2022-12-22 12:41:47
我正在重建一個網站,其中包含對格斗游戲《鐵拳 7 》中角色的基本介紹。我已將所有角色及其數據存儲為對象,并設置了一個函數來在網頁上顯示角色的數據,并接受該角色的名稱作為其唯一參數。/* DECLARATIONS */// Profilelet charName = document.getElementById("char-name");let charNickname = document.getElementById("nickname");let charFlag = document.getElementById("flag");let charImg = document.getElementById("image");lt charAge = document.getElementById("age");let charCountry = document.getElementById("country");let charFightingStyle = document.getElementById("fighting-style");let charDebut = document.getElementById("first-appearance");// Scoreslet charOffense = document.getElementById("offense");let charDefence = document.getElementById("defence");let charRange = document.getElementById("range");let charPunishment = document.getElementById("punishment");let charGimmicks = document.getElementById("gimmicks");let charExecution = document.getElementById("execution");let charHurtbox = document.getElementById("hurtbox");// Playstyle and Introlet charPlaystyle = document.getElementById("playstyle");let charIntro = document.getElementById("introduction");/* DISPLAY FUNCTION */const display = character => {    charName.innerHTML = character.name;    charNickname.innerHTML = character.nickname;    charFlag.src = character.flag;    charImg.src = character.image;    charAge.innerHTML = character.age;    charCountry.innerHTML = character.country;    charFightingStyle.innerHTML = character.fightingStyle;    charDebut.innerHTML = character.debut;}該代碼從地址欄中解析出“view”參數,并將其作為參數返回給函數。例如,如果地址欄有 URL .../guides/character.html?view=jin,理想情況下,代碼應該解析該jin值并將其作為參數傳回函數以顯示 this。我什至用 測試了這個char參數,console.log看看這個值是否順利傳遞并且jin按預期打印。但是,當代碼自行運行時,它無法以某種方式使用該值作為參數,而是傳回一個未定義的對象,控制臺顯示GET [file path]/guides/undefined net::ERR_FILE_NOT_FOUND如下所示的錯誤消息。誰能幫我理解為什么會這樣?我仍在學習 JavaScript 的一些內部工作原理,所以我完全被難住了。
查看完整描述

1 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

你非常接近這個正確。我相信您面臨的問題是您希望字符串 of"jin"引用您的const jin. 然而,這并不是 JS 渲染引擎的工作方式——字符串 of"jin"只是作為字符串傳遞,這就是為什么你的所有值都顯示為未定義——因為字符串"jin"沒有你正在尋找的任何屬性。


這將記錄傳遞的字符串"jin",然后是幾個undefined:


const jin = {

    // Profile

    name: "Jin Kazama",

    nickname: "The Child of Destiny",


    flag: "../img/flagicons/japan.svg",

    image: "../img/characters/jin.png",


    age: 21,

    country: "Japan",

    fightingStyle: "Traditional karate",

    debut: "<em>Tekken 3</em>",


    // Scores

    offense: 9,

    defence: 10,

    range: 8,

    punishment: 8,


    gimmicks: 3,

    execution: 3,

    hurtbox: 3,


    // Playstyle

    playstyle: "Versatile, keep-out, Mishima",

    introduction: "<p>Versatile character who performs at his best in the mid-range, armed with good poking, great counter hit tools, great damage output, variety in his throws and a unique parry that deals with all highs and mids except projectiles (fireballs). While his Mishima-style tools are not quite as effective as those of the out-and-out Mishima characters, he makes up for it with other situational moves that plug those weaknesses. He does, however, lack range on a few key punishers.</p>",

};


const display = character => {

    console.log(character);

    console.log(character.name);

    console.log(character.nickname);


    console.log(character.flag);

    console.log(character.image);


    console.log(character.age);

    console.log(character.country);

    console.log(character.fightingStyle);

    console.log(character.debut);


    console.log(character.offense);

    console.log(character.defence);

    console.log(character.range);

    console.log(character.punishment);


    console.log(character.gimmicks);

    console.log(character.execution);

    console.log(character.hurtbox);


    console.log(character.playstyle);

    console.log(character.introduction);

}


display('jin');

那么如何解決呢?最有可能的最簡單方法是創建一個名為 的巨型配置對象characters,其中包含每個角色名稱的屬性,其中包含一個具有所有屬性的對象。通過使用對象,您可以通過字符串引用字符來獲取具有所有屬性的對象:


顯示整個對象,然后是各個統計信息/屬性:


const characters ={

    jin: {

        // Profile

        name: "Jin Kazama",

        nickname: "The Child of Destiny",


        flag: "../img/flagicons/japan.svg",

        image: "../img/characters/jin.png",


        age: 21,

        country: "Japan",

        fightingStyle: "Traditional karate",

        debut: "<em>Tekken 3</em>",


        // Scores

        offense: 9,

        defence: 10,

        range: 8,

        punishment: 8,


        gimmicks: 3,

        execution: 3,

        hurtbox: 3,


        // Playstyle

        playstyle: "Versatile, keep-out, Mishima",

        introduction: "<p>Versatile character who performs at his best in the mid-range, armed with good poking, great counter hit tools, great damage output, variety in his throws and a unique parry that deals with all highs and mids except projectiles (fireballs). While his Mishima-style tools are not quite as effective as those of the out-and-out Mishima characters, he makes up for it with other situational moves that plug those weaknesses. He does, however, lack range on a few key punishers.</p>",

    }

};


const display = character => {

    console.log(character);

    console.log(character.name);

    console.log(character.nickname);


    console.log(character.flag);

    console.log(character.image);


    console.log(character.age);

    console.log(character.country);

    console.log(character.fightingStyle);

    console.log(character.debut);


    console.log(character.offense);

    console.log(character.defence);

    console.log(character.range);

    console.log(character.punishment);


    console.log(character.gimmicks);

    console.log(character.execution);

    console.log(character.hurtbox);


    console.log(character.playstyle);

    console.log(character.introduction);

}


display(characters['jin']);


查看完整回答
反對 回復 2022-12-22
  • 1 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號