2017-12-30 11 views
0

.src를 사용하려면 어떻게해야합니까? 내 목표는 이미지의 src를 특정 링크로 설정하는 것입니다 (링크는 id가 무엇인지에 따라 변경됩니다). 더 쉬운 방법이 있는지 모르겠습니다. 나는이 작업을 수행 할 때HTMLElement to HTMLImageElement

property src does not exist on type HTMLElement

:

나는 오류를 받고 있어요

document.getElementById("icon" + i).src = "http://openweathermap.org/img/w/" + weatherIconIds[i] + ".png" 

나는 어떻게 든 내는 HTMLElement를 변환 할 필요가 다른 StackOverflow의 게시물에서 발견했습니다 HTMLImageElement,하지만 그렇게하는 데 문제가 있습니다. 다른 게시물은 변수를 HTMLImageElement로 선언하는 것이 좋지만 HTML에서 변수를 선언 할 필요는 없었습니다. 그와 관련된

HTML은 다음과 같습니다

<img src="" id="icon0" alt="" height="40" width="40"> 

답변

0

이 시도 :

document.getElementById("icon" + i).setAttribute("src","http://openweathermap.org/img/w/" + weatherIconIds[i] + ".png"); 
+0

정말 감사합니다! 그것은 내 문제를 해결하고 해결했습니다. –