一個詞:國家。您的 HTML 頁面沒有顯示正確的按鈕顏色,因為您沒有在頁面刷新之間維護狀態。就目前而言,即使您在燈已經亮起時加載頁面,它仍然不會向您顯示綠色按鈕。最簡單的解決方法是使用 JS 維護 cookie 或本地存儲中的燈光狀態,在頁面加載時讀取此狀態,然后為按鈕顯示適當的顏色。// on clicking the button, do this:localStorage.setItem('isLightOn', '1');// and on page load, do this:if (localStorage.getItem('isLightOn') === '1') { $('button').addClass('selected');} else { $('button').removeClass('selected');}這樣做的缺點是它不能根據服務器反映燈光的當前狀態。為了始終在頁面中顯示正確的燈光狀態,您需要一種機制來從服務器查詢燈光的當前狀態。然后您可以從服務器讀取狀態,然后顯示相應的 HTML。您甚至可以將其放入索引文件中。調用它index.php并使用與您的 中相同的內容index.html,并添加一些 PHP:<?php # Let's assume this returns `true` when light is on, and `false` is it's off. $lightState = @file_get_contents('http://1.1.1.1/cgi-bin/output?username=abcd&password=cdef&action=read&pin=relay'); if ($lightState == true):?> <button class="button selected">Lights On</button><?php else:?> <button class="button">Lights On</button>我可能對語法有點生疏,但這就是總體思路。希望有幫助!
Vue,帶有 true/false 布爾值的 v-select 組件
江戶川亂折騰
2023-08-10 15:53:29