识别没有 ID 或特定属性的对象
Identify Objects without ID or specific attributes
我正在尝试通过悬停在元素上方来创建弹出窗口。弹出窗口应在第一次悬停后创建/加载。
初始后的每次悬停不应创建新元素,而应重新打开已创建的元素。
我正在徘徊的元素 没有 ID 或 class 因此,例如,我无法将它们存储在数组中。
$( ".tooltip-enabled" ).hover(
function() {
// Tooltip already exists?
// ...
createToolTip($(this));
}, function() {
closeToolTip($(this));
}
);
显示的代码将始终创建一个新的工具提示。有没有办法存储我已经悬停在上面的哪个对象?
The shown code will always create a new Tooltip. Is there a way to
store which object I already hovered above?
是的。您可以使用 $.fn.data
来存储一个计数器。
$(".tooltip-enabled").hover(function() {
if ($(this).data('hovered'))
createToolTip($(this));
else {
$(this).data('hovered', true); // set it here
openToolTip($(this));
}
}, function() { closeToolTip($(this)); });
请注意,为了使代码简洁,我省略了 if
和 else
两边的括号。如果您的代码跨越多个语句,则需要使用方括号。
我正在尝试通过悬停在元素上方来创建弹出窗口。弹出窗口应在第一次悬停后创建/加载。
初始后的每次悬停不应创建新元素,而应重新打开已创建的元素。
我正在徘徊的元素 没有 ID 或 class 因此,例如,我无法将它们存储在数组中。
$( ".tooltip-enabled" ).hover(
function() {
// Tooltip already exists?
// ...
createToolTip($(this));
}, function() {
closeToolTip($(this));
}
);
显示的代码将始终创建一个新的工具提示。有没有办法存储我已经悬停在上面的哪个对象?
The shown code will always create a new Tooltip. Is there a way to store which object I already hovered above?
是的。您可以使用 $.fn.data
来存储一个计数器。
$(".tooltip-enabled").hover(function() {
if ($(this).data('hovered'))
createToolTip($(this));
else {
$(this).data('hovered', true); // set it here
openToolTip($(this));
}
}, function() { closeToolTip($(this)); });
请注意,为了使代码简洁,我省略了 if
和 else
两边的括号。如果您的代码跨越多个语句,则需要使用方括号。