【发布时间】:2017-05-09 04:12:53
【问题描述】:
我想使用 Ajax 从 JavaScript 更改 img 的来源。而img 标签没有任何 ID 或类,我不知道如何选择img 以及如何更改src。我怎样才能做到这一点?我把我的 HTML 的一部分(它提供了,所以我不能修改)和我的 JavaScript 的一部分。谢谢。
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吗?
-
我根本无法修改 html。我想改变所有的图像
标签: javascript html ajax src