在我的應用程序中,打開深色模式功能后,我希望將一些圖像更改為其他圖像。但當圖像由兩張圖片組成時,這種情況就不會發生,然后此后的其余圖像也不會發生變化,這是清楚描述問題的圖像。這是 PHP 和 javascript 代碼: <?php } else if ($rating_avg > 4.75 && $rating_avg < 6) { ?> <form class="star-form" action="star_frame.php?post_id=<?= $post_id ?>" method="POST"> <div class='stars_avg_div' align='center'> <i class='star1' id='star1' style='width:20px; height:20px; '><img class='star1img' src='starou.png' style='width:20px; height:20px;background-color:gold; '></i> <i class='star2' id='star2' style='width:20px; height:20px; '><img class='star2img' src='starou.png' style='width:20px; height:20px;background-color:gold;'></i> <i class='star3' id='star3' style='width:20px; height:20px; '><img class='star3img' src='starou.png' style='width:20px; height:20px;background-color:gold;'></i> <i class='star4' id='star4' style='width:20px; height:20px; '><img class='star4img' src='starou.png' style='width:20px; height:20px;background-color:gold;'></i> <i class='star5' id='star5' style='width:20px; height:20px; '><img class='star5img' src='starou.png' style='width:20px; height:20px;background-color:gold;'></i> </div> </form> <?php } ?> function darkmode() { $('*').addClass('dark'); $('avg_iframe').addClass('dark'); }頁面加載時,控制臺面板顯示錯誤“TypeError:document.getElementsByClassName(...)[0] 未定義 star_avg_frame.php:88:22(參考最后幾行代碼)”我也嘗試過getElementsByIdName,getElementsByClassName但不是這樣。您知道為什么會發生圖像上顯示的情況嗎?我必須補充一點,如果沒有“半星”,一切都會很好。在此先感謝您的幫助!
1 回答

呼喚遠方
TA貢獻1856條經驗 獲得超11個贊
您可以使用querySelector(".star1img")
代替getElementsByClassName
,并獲得第一個元素class="star1img"
這document.getElementById("half_star_1")[0].src = "starou_half_1_dark.png"
沒有任何意義,因為getElementById
你得到的是 1 個元素,而不是數組。所以你不需要在它后面使用[0]。另外,頁面上沒有具有此 id 的元素
- 1 回答
- 0 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消