


How to dynamically modify the hover color of HTML elements by clicking the button?
Apr 04, 2025 pm 03:42 PMUsing CSS variables and JavaScript to dynamically modify element hover color
This article introduces how to dynamically change the hover color of HTML elements by clicking a button to improve the web interactive experience. We will use CSS variables and JavaScript to implement this functionality.
The goal is: click the button to modify the existing element hover style. To do this, we will use CSS variables to define the hover color and modify the value of the CSS variable in the button click event through JavaScript.
Implementation plan:
First, the HTML structure contains the target element ( .element
) and multiple buttons ( .change-color
), each button corresponding to a hover color:
<div class="container"> <div class="element"></div> <button class="change-color">red</button> <button class="change-color">green</button> <button class="change-color">blue</button> </div>
CSS style defines the basic style of the element and uses the CSS variable --color
to control the hover color, default red:
.element { width: 100px; height: 100px; /* aspect-ratio is not compatible with all browsers, changed to fixed height*/ margin: 20px; background-color: lightgray; /* Add background color to make it easier to observe the effect*/ } .element:hover { background: var(--color, red); }
Finally, the JavaScript code listens for each button click event. After clicking the button, get the button text content (color name) and assign the color value to the CSS variable --color
using el.style.setProperty()
method:
const el = document.querySelector(".element"); document.querySelectorAll(".change-color").forEach(e => { e.addEventListener("click", () => { el.style.setProperty("--color", e.textContent); }); });
Through the collaborative work of HTML, CSS and JavaScript, you can realize the function of dynamically changing the hover color of HTML elements by clicking a button. This method is simple, efficient and easy to maintain.
The above is the detailed content of How to dynamically modify the hover color of HTML elements by clicking the button?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

?In today's increasingly popular digital asset trading, it is particularly important to understand how to safely and effectively withdraw your digital assets. As a world-renowned digital asset trading platform, ok exchange provides convenient currency withdrawal services. This tutorial will analyze in detail the steps of withdrawing coins on the ok exchange, things to note, and some common questions and answers to help users complete asset transfer smoothly and ensure asset security. Whether you are using ok exchange for withdrawals for the first time or want to further understand the withdrawal process, this article will provide you with clear and practical guidance.

The latest version of Digital Currency Exchange 2025 is a world-leading digital asset trading application, committed to providing users with secure, stable and convenient trading services for mainstream digital currencies such as Bitcoin and Ethereum. Its simple and intuitive operation interface and powerful functional modules make it easy for both novices and senior traders to get started.

Decentralized exchanges (DEXs) have attracted attention in the cryptocurrency field in recent years. Unlike traditional centralized exchanges (CEX), DEX operates on blockchain and aims to provide a way to trade without trusting intermediaries. When a user trades on a DEX, the assets are usually kept in their wallet rather than stored in an escrow account on the exchange. This model brings unique security advantages, but it also comes with new challenges.

The latest version of Binance is v2.102.5, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

The latest version of Binance is v2.102.5, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

As an emerging thing, digital assets are developed with opportunities and challenges. Understanding and avoiding the risks brought by illegal digital assets such as "black USDT" is an unshirkable responsibility for every participant. By raising risk awareness, mastering identification skills, and insisting on trading under the compliance framework, we can not only better protect our property safety, but also contribute to building a healthier, transparent and sustainable digital asset market. Safety first, prudent operation is the golden rule of the navigating digital asset world.

The latest version of Binance is 2.101.8, and the update tutorial is: 1. Click the download link in the web page; 2. Authorize the installation permission of "Allow installation from unknown sources"; 3. Find the downloaded APk and click to install; 4. Click the installed application to open it.

The three types of cryptocurrencies that may be liquidated or restricted by regulators in 2025 include: 1. Privacy currency, which is easily used for illegal activities due to its anonymity and faces restrictions or removal from the shelves caused by the strengthening of global AML/KYC regulations; 2. Unregistered securities tokens may be removed from the exchange or required rectification due to non-compliance with securities regulations; 3. Non-compliant stablecoins may be prohibited from trading due to lack of transparent reserves or regulatory permissions. It is recommended to pay close attention to regulatory trends, diversified investment, pay attention to exchange announcements and consult professionals to deal with policy risks.
