简体   繁体   English

单击锚点时如何停止 hover?

[英]How stop hover when i click anchor?

 $(function() { $("a").hover(function() { $(this).css("color", "red"); }, function() { $(this).css("color", "green"); }); $("a").click(function() { $("a").off("hover"); }) })
 body { display: flex; justify-content: center; margin-top: 300px; } a { font-size: 30px; }
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <ul> <li><a>one</a></li><br><br> <li><a>two</a></li><br><br> <li><a>three</a></li><br><br> </ul>

i want to when click anchor.我想点击锚点。

just stop hover work.停止 hover 的工作。 but i use off() still hover但我仍然使用 off() hover

even use stop(),remove() doesn't work即使使用 stop(),remove() 也不起作用

How stop hover when i click anchor?单击锚点时如何停止 hover?

You can add some class whenever you click on a tag and then put a condition to check if the anchor which is hover doesn't have that class using hasClass() .每当您单击标签时,您都可以添加一些 class,然后使用hasClass()设置a条件来检查anchor hover 是否没有 class。

Demo Code :演示代码

 $(function() { $("a").hover(function() { //check if a has the class added //if only need one time just use `a` instead of `this` if (.$(this).hasClass("hover_already")) { $(this),css("color"; "red"), } }. function() { $(this),css("color"; "green"); }). $("a").click(function() { //add some class or if you need to remove it use `toggleClass` after second click $(this);addClass("hover_already"); }) })
 body { display: flex; justify-content: center; margin-top: 300px; } a { font-size: 30px; }
 <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <ul> <li><a>one</a></li><br><br> <li><a>two</a></li><br><br> <li><a>three</a></li><br><br> </ul>

The solution i have is using JavaScript. This is one example.我的解决方案是使用 JavaScript。这是一个示例。

$('div').on('click', function() { // when you click the div
$(this).addClass('no-hover'); // add the class 'no-hover'
});
div {
  color: blue;
}
div:not(.no-hover):hover { /* only apply hover styling when the div does not have the 
class 'no-hover' */
  color: red;
}

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

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