Javascript 如何防止重写:将鼠标悬停在上。单击“操作”
我需要:悬停并单击“一起工作”Javascript 如何防止重写:将鼠标悬停在上。单击“操作”,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我需要:悬停并单击“一起工作” <div class="d">...</div> $('.d').click(function() { $(this).parent().find('.d').css('background-color', 'white'); $(this).css('background-color', 'red') }); 单击div时,悬停效果消失。单击div时,您应该添加一个类(在示例中用作类名)。您的问题出现在添加内联样式属性
<div class="d">...</div>
$('.d').click(function()
{
$(this).parent().find('.d').css('background-color', 'white');
$(this).css('background-color', 'red')
});
单击div时,悬停效果消失。单击
div
时,您应该添加一个类(在示例中用作类名)。您的问题出现在添加内联样式属性时,该属性的优先级高于CSS类
CSS
.d{
背景色:白色;
}
.d:悬停,。单击{
背景色:红色;
}
剧本
或
您可以使用!使用.d:hover
类进行重要操作
CSS
.d:悬停{
背景色:红色!重要;
}
您也可以使用:
$('.d').click(function () {
$(this).parent().find('.d').css('background-color', '');
$(this).css('background-color', 'red')
});
从元素样式中“删除”背景样式
发件人:
将style属性的值设置为空字符串-例如$(“#mydiv”).css(“color”),如果该属性已直接应用于某个元素,则会从该元素中移除该属性,无论是在HTML样式属性中,还是通过jQuery的.css()方法,还是通过style属性的直接DOM操作
不要直接通过JS设置CSS属性–而是设置类名。
$('.d').click(function () {
$(this).parent().find('.d').css('background-color', '');
$(this).css('background-color', 'red')
});