简体   繁体   English

如何防止重写:将鼠标悬停在.click动作上

[英]How to prevent rewrite :hover on .click action

I need :hover and .click work together. 我需要:hover和.click一起工作。

<div class="d">...</div>

$('.d').click(function()
{
    $(this).parent().find('.d').css('background-color', 'white');
    $(this).css('background-color', 'red')
});

and css: 和CSS:

.d {
    background-color: white;
}
.d:hover {
    background-color: red;
}

When I click on the div hover effect disappear. 当我单击div时,悬停效果消失。

You should add a class when div is clicked(used as class name in example). 单击div时应添加一个类(在示例中用作类名)。 Your problem arises as .css() add inline style attribute which has priority over CSS class. .css()添加优先于CSS类的内联样式属性时,就会出现您的问题。

CSS 的CSS

.d {
    background-color: white;
}
.d:hover, .clicked {
    background-color: red;
}

Script 脚本

$('.d').click(function () {
    $(this).parent().find('.d').removeClass('clicked');
    $(this).addClass('clicked')
});

DEMO 演示

OR 要么

You can use !important with the .d:hover class 您可以使用!important.d:hover

CSS 的CSS

.d:hover{
    background-color: red!important;
}

DEMO 演示

You can also just use: 您也可以使用:

$('.d').click(function () {
    $(this).parent().find('.d').css('background-color', '');
    $(this).css('background-color', 'red')
});

to "remove" the background style from the element style. 从元素样式中“删除”背景样式。

from: http://api.jquery.com/css/#css-propertyName-value 来自: http : //api.jquery.com/css/#css-propertyName-value

Setting the value of a style property to an empty string — eg $( "#mydiv" ).css( "color", "" ) — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. 将样式属性的值设置为空字符串,例如$(“ #mydiv”).css(“ color”,“”)-如果该属性已被直接应用(无论是HTML样式),则将其从元素中删除。属性,通过jQuery的.css()方法或通过对style属性的直接DOM操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM