img
のソースをJavaScriptからAjaxを使用して変更したいと思います。 img
タグにIDやクラスがないため、img
の選択方法とsrc
の変更方法を理解できません。どうやってやるの?私はHTML(提供されているので変更できません)とJavaScriptの一部を入れています。ありがとう。JavaScriptからクラス/ IDを持たない画像のsrcを変更する方法
HTML:
<div class="moves">
<button>
<span class="move">Move Name Here</span> <span class="dp"></span>
<img src="icons/fighting.jpg" alt="Pokemon move" />
</button>
<button>
<span class="move">Move Name Here</span> <span class="dp"></span>
<img src="icons/fighting.jpg" alt="Pokemon move" />
</button>
<button>
<span class="move">Move Name Here</span> <span class="dp"></span>
<img src="icons/fighting.jpg" alt="Pokemon move" />
</button>
<button>
<span class="move">Move Name Here</span> <span class="dp"></span>
<img src="icons/fighting.jpg" alt="Pokemon move" />
</button>
</div>
Javascriptを:
var moveArr = data.moves;
var moves = document.getElementsByClassName("move");
for(var x=0; x < data.moves.length; x++){
moves[x].innerHTML = moveArr[x].name;
img[x].src = "icons\/" + moveArr[x].type + ".jpg"; // I cannot write this part correctly.
}
どの画像を変更しますか?あなたはhtmlをまったく変更できますか? – gaganshera
私はhtmlをまったく変更できません。そして、私はすべての画像を変更したい –