我正在尝试将自定义属性传递给脚本。如果我这样传递:
$('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/