儘管這可能很安靜,但我在Web開發中只有幾個月的時間,並且我死在這裏。我有一個頁面,多個圖像項目設置爲一個類。通過這個,我的意思是下面的代碼如何爲多個項目的類設置屬性值
<img src="http://img1.gif" id="GA2" class="CHAngeR" />
<img src="http://img1.gif" id="GA2" class="CHAngeR" />
我想執行一個函數,編輯/附加圖像的「src」屬性。
我試過使用相同的ID,但我發現W3C的猜測是相反的,我也嘗試了下面的代碼,但仍然無法正常工作,我該怎麼做。
<!DOCTYPE html>
<html>
<head>
<title>DOWNLOAD PAGE</title>
<input type="button" value="change image" id="iMgChAnGe" onclick="changeIMG()"/>
<div class="changer">
<img src="http://img1.gif" id="GA1" class="CHAngeR" />
<img src="http://img2.gif" id="GA2" class="CHAngeR" />
<img src="http://img3.gif" id="GA3" class="CHAngeR" />
<img src="http://img4.gif" id="GA4" class="CHAngeR" />
<img src="http://imgl.gif" id="GA5" class="CHAngeR" />
</div>
<script>
function ChangeIMG(){
document.getElementByClassName(CHAngeR).setAttribute("src","http://google.com/img/images/static.gif");
}
</script>
</head>
</html>
你的問題是一團糟。您需要清理並正確格式化。另外:它是JavaScript,而不是Java。這些是兩種不同的語言 –
歡迎來到堆棧溢出。您可以使用工具欄按鈕格式化源代碼,並查看底部實時預覽面板中的問題。另外,您可以編輯問題以改進問題。它通常會爲您重新格式化問題,但所有代碼都在一行中。 –