我想通过使用Ajax从JavaScript更改img
的源代码。 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。我想改变所有图像 –