简体   繁体   English

添加切换到 jQuery mouseover 事件以触发 CSS 颜色更改

[英]Add toggle to jQuery mouseover event to trigger CSS color change

I have below script that works fine when the mouse is hover over the #eventID then the #targetID text color turns purple.当鼠标悬停在#eventID然后#targetID文本颜色变为紫色时,我有以下脚本可以正常工作。 I hope to add a toggle to the script so that when mouseout the text color changes back to its original or defined color.我希望在脚本中添加一个切换,以便当mouseout时文本颜色变回其原始颜色或定义的颜色。

$("#eventID").on ("mouseover",function(){
  $("#targetID").css("color","purple");
})

I tried to add the toggle before the css function and before the jQuery event listener, but neither of them works.我尝试在 css 函数和 jQuery 事件侦听器之前添加切换,但它们都不起作用。

Test1:测试1:

$("#eventID").on ("mouseover mouseout",function(){
  $("#targetID").toggle(css("color","purple"));})

Test2:测试2:

$("#eventID").toggle(on("mouseover mouseout", function() {
  $("#targetID").css("color", "purple");}));

You can use:您可以使用:

$("#eventID").on("mouseover", function() { 
   $("#targetID").css("color","purple");
}).on("mouseout", function() {
   $("#targetID").css("color","");
});

This will clear the inline color style.这将清除内联颜色样式。 when you mouseout.当你鼠标悬停时。

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

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