Remove/Add hover after toggle Jquery

左心房为你撑大大i 提交于 2019-12-11 04:48:05

问题


I have this css:

.disable {
          properties
          }

.comment_action:hover {
                       properties
                       }

I have this jQuery:

//comment button index disable
$("#mydiv").toggle(function() {
 $(this).find(".comment_action").addClass('disable');   
},
function(){
 $(this).find(".comment_action").removeClass('disable');
});

The problem for me is that when I doing click the .comment_action:hover is not disappear or removed. I want that If I doing click on the class .comment_action:hover dissapear and if I doing click again the .comment_action:hover appear.


回答1:


You would need an !important added to properties you want to override in the :hover psuedo-selector...

Because :hover takes precedence, even if .disabled is applied.

Also your javascript should be calling find with .comment_action instead of comment_action

See working example: http://jsfiddle.net/TN4rh/11/




回答2:


Better solution would be toggle the class. What you are trying to do can be done in one line.

$("#mydiv").click(function() {
    $(this).find(".comment_action").toggleClass('disable');       
});

Demo

:)




回答3:


You are telling your browser to add hover effects to .comment_action. This is what is going to happen to all elements with that class, regardless of it having any other class such as .disable.

Solution: add the :hover properties to a new .enable class. Instead of just removing and adding .disable, also add and remove .enable.

.disable {
          properties
          }

.enable:hover {
                properties
                }


来源:https://stackoverflow.com/questions/8679246/remove-add-hover-after-toggle-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!