gpt4 book ai didi

javascript - 使用 jQuery 将鼠标悬停在相邻文本上时更改图标的颜色

转载 作者:行者123 更新时间:2023-12-03 08:28:30 25 4
gpt4 key购买 nike

我见过this article但我想用 JavaScript 更改图标的颜色。

尝试编写一个函数,当图标悬停在文本上时,更改图标文本的颜色。

我正在使用 this answer 中提供的代码检查元素是否通过 jQuery 悬停:

function changeIconColor (hoverState) {
let contactText = document.getElementsByClassName('contact-text');
let contactIcon = document.getElementsByClassName('contact-icon');
//if the text is hovered over, change the color of the icon to #e46e6e
if ($('#contact-text').is(":hover")) {
contactIcon.css("color", "red");
};
if ($('#contact-icon').is(":hover")) {
contactText.css("color", "red");
};
}

changeIconColor();
.outer-one {
display: flex;
flex-direction: row;
}

.outer-two {
display: flex;
}

.phone-text, .contact-text {
color: #213b56;
text-decoration: none;
font-weight: bold;
font-family: 'Raleway';
margin-top: 4px;
}

.contact-text {
margin-top: 7px;
}

.contact-text:hover {
color: #e46e6e;
}

.user-icon, .contact-icon {
padding: 7px;
}
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>

<div class="outer-one">
<div class="outer-two">
<i class="far fa-user user-icon"></i>
<span class="phone-text" style="font-family: Raleway, sans-serif; font-size:
1.2rem;">(314) 567-7000  | </span>
<i class="far fa-envelope contact-icon" id="contact-icon"></i>
<a class="contact-text" href="http://cfk2021.flywheelsites.com/" id="contact-text">CONTACT</a>
</div>
</div>

如您所见,我正在 <head> 中加载 jQuery html的,所以我不确定为什么会出现脚本错误。或者看看这个jSFiddle供引用。

最佳答案

您在 jQuery 上使用了错误的伪选择器,因此出现错误。

使用hover()相反,要改变颜色,知道它有两个函数作为参数,第一个用于 mouseenter,第二个用于 mouseleave

请参阅以下代码片段:

1-将鼠标悬停应用于相同的文本和图标(较短版本):

$(function() {
$('.contact-text, .contact-icon').hover(
function() {
$('.contact-text, .contact-icon').css("color", "#e46e6e");
},
function() {
$('.contact-text, .contact-icon').css("color", "#213b56");
}
)
});
.outer-one {
display: flex;
flex-direction: row;
}

.outer-two {
display: flex;
}

.outer-three {
display: flex;
}

.phone-text,
.contact-text {
color: #213b56;
text-decoration: none;
font-weight: bold;
font-family: 'Raleway';
margin-top: 4px;
}

.contact-text {
margin-top: 7px;
}

.contact-text:hover {
color: #e46e6e;
}

.user-icon,
.contact-icon {
padding: 7px;
}
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous" />

<div class="outer-one">
<div class="outer-two">
<i class="far fa-user user-icon"></i>
<span class="phone-text" style="font-family: Raleway, sans-serif; font-size:
1.2rem;">(314) 567-7000  | </span>
<i class="far fa-envelope contact-icon"></i>
<a class="contact-text" href="http://cfk2021.flywheelsites.com/">CONTACT</a>
</div>
</div>

2- 悬停分别应用于文本和图标:

//
let $contactText = $('.contact-text');
let $contactIcon = $('.contact-icon');

$contactText.hover(
function() {
$contactIcon.css("color", "#e46e6e");
},
function() {
$contactIcon.css("color", "#213b56");
}
)

$contactIcon.hover(
function() {
$contactText.css("color", "#e46e6e");;
},
function() {
$contactText.css("color", "#213b56");
}
)
.outer-one {
display: flex;
flex-direction: row;
}

.outer-two {
display: flex;
}

.outer-three {
display: flex;
}

.phone-text,
.contact-text {
color: #213b56;
text-decoration: none;
font-weight: bold;
font-family: 'Raleway';
margin-top: 4px;
}

.contact-text {
margin-top: 7px;
}

.contact-text:hover {
color: #e46e6e;
}

.user-icon,
.contact-icon {
padding: 7px;
}
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous" />

<div class="outer-one">
<div class="outer-two">
<i class="far fa-user user-icon"></i>
<span class="phone-text" style="font-family: Raleway, sans-serif; font-size:
1.2rem;">(314) 567-7000  | </span>
<i class="far fa-envelope contact-icon"></i>
<a class="contact-text" href="http://cfk2021.flywheelsites.com/">CONTACT</a>
</div>
</div>

关于javascript - 使用 jQuery 将鼠标悬停在相邻文本上时更改图标的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65924975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com