我試圖用 .innerHTML 顯示兩個按鈕,但它不顯示放在字串中的內容。控制臺沒有顯示任何錯誤,我檢查了拼寫錯誤,但我什么也沒找到。
這是我的 HTML :
<div class="buttons">
<div class="buttons_inner" id="buttonShiny"></div>
</div>
這是我的 JS :
const shiny = document.getElementById('buttonShiny');
const displayButtonShiny = (pokemon) => {
const shinyHTMLString = `
<button class="buttons_shiny" onclick="document.getElementById('pokemonSprite').src='https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/${pokemon.id}.png'">Shiny</button>
<button class="buttons_normal" onclick="document.getElementById('pokemonSprite').src='https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png'">Normal</button>
`;
shiny.innerHTML = shinyHTMLString;
};
ShinyHTMLString 中的內容應該顯示在#buttonShiny div 中,但它不起作用
呼叫 JS 檔案的腳本標簽就在結束正文標簽之前
uj5u.com熱心網友回復:
是的,您宣告了該函式,但從不使用它。僅添加displayButtonShiny("x")到您的 js 代碼中。
const shiny = document.getElementById('buttonShiny');
const displayButtonShiny = (pokemon) => {
const shinyHTMLString = `
<button onclick="document.getElementById('pokemonSprite').src='https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/shiny/${pokemon.id}.png'">Shiny</button>
<button onclick="document.getElementById('pokemonSprite').src='https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png'">Normal</button>
`;
shiny.innerHTML = shinyHTMLString;
};
displayButtonShiny("x"); // x = your pokemon
<div class="buttons">
<div class="buttons_inner" id="buttonShiny">btn1</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/401764.html
標籤:javascript html 内页
上一篇:更改輪播上div的背景顏色
