javascript - JS 双鼠标悬停

标签 javascript jquery this mouseover

我正在尝试将自定义属性传递给脚本。如果我这样传递:

$('a.load-local-image').cluetip( {
    local:true, 
    leftOffset: $("#par-1").attr("leftpos")
);

效果很好。但是,我需要传递当前元素的属性,而不仅仅是 par-1。如果我这样尝试:

$('a.load-local-image').cluetip( {
    local:true, 
    leftOffset: $(this).attr("leftpos")
);

函数认为参数根本没有被传递。因此,我尝试将函数包装在 mouseenter() 中:

$('a.load-local-image').mouseenter( {
    $(this).cluetip( {
        local:true, 
        leftOffset: $(this).attr("leftpos")
    });
});

它工作得很好,只是在线索提示脚本运行之前我必须将鼠标悬停两次。我什至尝试以这种方式预加载线索提示():

$('a.load-local-image').cluetip();
$('a.load-local-image').mouseenter( {
    $(this).cluetip( {
        local:true, 
        leftOffset: $(this).attr("leftpos")
    });
});

它仍然做同样的事情。我还尝试了 mouseover、mouseout、mouseleave、hover 以及我能想到的所有其他与鼠标相关的功能。

出于某种莫名其妙的原因,问题似乎与$(this)有关。如果我使用除 $(this) 之外的任何内容作为参数,则一切正常。不幸的是,我传递当前元素的唯一方法是 $(this) ,除非有人知道我如何才能以其他方式获取它。

这是标记:

<div id="par-1">
    <a id="load-local" class="load-local-image featurelink" title="" href="" rel="#whatever" leftpos="220" toppos="48">
    <img src="images/image.jpg" alt="" class="featureimg"></a> 

对正在发生的事情有什么想法吗?

最佳答案

this传递给cluetip将意味着全局窗口对象,而不是当前元素。要获取当前元素,请将其放入 each 语句中并一一初始化每个线索提示:

$('a.load-local-image').each(function () {
    var element = $(this);

    element.cluetip({
        local:true, 
        leftOffset: element.attr("leftpos")
    });
});

关于javascript - JS 双鼠标悬停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19867363/

相关文章:

javascript - 如何在jquery函数中找到引用的对象

c++ - 当方法参数具有相同的名称时,我们如何引用字段?

JavaScript/Ajax 鼠标可选择对象

javascript - 使用 HTML 或 JavaScript 重命名后下载文件

javascript - 在附加选择字段上使用 select2 时超出最大调用堆栈大小

javascript - jQuery Load 和 Ajax 表单不能一起工作

javascript - 如何在另一个函数中使用 $(this) 引用?

javascript - ES6 rest 参数不适用于 babel

javascript - 提交按钮同时使用两种方法,而不是一种方法,我该如何解决这个问题?

javascript - 标签 &lt;script&gt; 内的代码在 VScode、Angular 的 component.html 文件中不起作用