How to compare colors in JavaScript?
Your code compares the background color of an element to a specific value (#ECECF4). However, this approach is not recommended as it tightly couples the business logic with the presentation.
Instead, consider keeping the logic in JavaScript and using classes to visually represent different states. This allows you to separate the styling from the business logic and ensure that the presentation is handled solely by CSS.
For example, you can use jQuery to toggle an 'active' class when an element is clicked:
$(".list").on("click", "li", function() { $(this).toggleClass('active'); });
Then, define the corresponding CSS to change the background color of active elements:
.list { width: 100%; padding: 0; } .list li { padding: 5px 10px; list-style: none; cursor: pointer; } .list li:hover { background-color: rgba(0, 0, 0, 0.05); } .list li.active { background-color: #eeeecc; }
This approach allows you to manipulate the presentation without modifying the business logic, leading to cleaner and more maintainable code.
以上是如何在 JavaScript 中比較顏色而不直接比較背景顏色?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

前幾天我得到了這個問題。我的第一個想法是:奇怪的問題!特異性是關於選擇者的,而在符號不是選擇器,那麼...無關緊要?

在這篇文章中,我們將使用我構建和部署的電子商務商店演示來進行Netlify,以展示如何為傳入數據製作動態路線。這是一個公平的


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具