javascript mouseover/out 结合点击行为

标签 javascript jquery html css

我是编程新手,请大家多多包涵。下面是我的代码:

$(function(){
document.getElementById("custom_link").addEventListener("mouseover",function(){
document.getElementById("siteContent").contentDocument.getElementById("custom_div").classList.toggle('highlightDiv');
},false)})

$(function(){
    document.getElementById("custom_link").addEventListener("click",function(){
    document.getElementById("siteContent").contentDocument.getElementById("custom_div").classList.add('highlightDiv');
    },false)})

我想做的是:

  1. 当用户将鼠标悬停在“custom_link”上时,“custom_div”会突出显示。
  2. 当用户将鼠标移出“custom_link”时,“custom_div”处的突出显示将消失。
  3. 当用户点击“custom_link”时,“custom_div”再次高亮显示。但是,当用户将鼠标移出时,“highlightDiv”仍被添加到“custom_div”。

根据我的代码,它不能正常工作,因为悬停时的行为很奇怪。如果您能用完整的代码结构或 jsfiddle 示例向我解释,那就太好了。感谢您的提前帮助。

最佳答案

http://jsfiddle.net/ETrjA/2/

$('#custom_link').hover(function () {
    $('#custom_div').toggleClass('highlighted'); 
});

$('#custom_link').click(function (e) {
   $('#custom_div').addClass('highlighted');
   $(e.currentTarget).unbind('mouseenter mouseleave');
});

您只需要一个类highlighted,您可以通过e.currentTargetclick 事件回调中直接访问链接元素。

关于javascript mouseover/out 结合点击行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14828995/

相关文章:

javascript - 与 OrbitControls 结合转换 HTML 元素,使它们看起来随着场景中的对象旋转

javascript - 在 javascript/jquery 中获取 CSS 规则

javascript - 如何从表单获取输入/输出并将其存储在 JS 变量中?

html - 居中滚动文本

php - 无法使用来自 php 的电子邮件发送多个文本/变量

javascript - Angular8 - 从服务返回的对象,排序和显示

javascript - 查询 : fadein function not working

javascript - 未捕获的类型错误 : elem. 替换不是函数

javascript - 检查哪些数组元素具有空值

javascript - 无法在 html 表格的下拉列表中显示 JSON 数据