gpt4 book ai didi

jQuery 颜色变化只会短暂闪烁,不是永久性的

转载 作者:行者123 更新时间:2023-11-28 07:26:57 27 4
gpt4 key购买 nike

我试图让一个列表项在单击时永久更改字体颜色,在这种情况下从绿色变为黄色。但是,下面的代码在单击时只会闪烁黄色半秒钟,然后默认为绿色。正如预期的那样,下划线样式会永久应用:

<!DOCTYPE html>
<html>
<head>
<script type = "text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script>
$(document).ready(function(){
$('li').hover(function(){
$(this).css('color', 'white')
}, function(){
$(this).css('color', '')
});

$("#resources").click(function(){
$("#resources").css('text-decoration', 'underline');
document.getElementById("resources").style.color = "yellow";
});
});
</script>
<style>
#main{
margin: 0 auto;
text-align: center;
background: black;
color: green;
font-family: Courier;
position: relative;
top: 300px;
width: 100%;
}

li{
display: inline;
padding: 8px;
padding-right: 1em;
position: relative;
}
li:hover{
color: white;
}
ul{
text-align: center;
}

</style>
</head>
<body>
<div id = "main">
<ul id = "main_links">
<li id = "resources">Resources</li>
</ul>
</div>

</body>
</html>

最佳答案

尝试用 .mouseenter() , .mouseleave() 代替 .hover() ;在 #resources .click() 事件中调用 .off()$(this)

$(document).ready(function() {
$('li').mouseenter(function() {
$(this).css('color', 'white')
}).mouseleave(function() {
$(this).css('color', '')
});

$("#resources").click(function() {
$(this).css({
"text-decoration": "underline"
, "color": "yellow"
})
.off("mouseenter mouseleave")
});

});
#main {
margin: 0 auto;
text-align: center;
background: black;
color: green;
font-family: Courier;
position: relative;
top: 300px;
width: 100%;
}
li {
display: inline;
padding: 8px;
padding-right: 1em;
position: relative;
}
li:hover {
color: white;
}
ul {
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>


<body>
<div id="main">
<ul id="main_links">
<li id="resources">Resources</li>
</ul>
</div>
</body>

关于jQuery 颜色变化只会短暂闪烁,不是永久性的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31756696/

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