gpt4 book ai didi

javascript - 悬停和 if 语句仅触发一次

转载 作者:行者123 更新时间:2023-11-28 21:08:37 26 4
gpt4 key购买 nike

mouseover 应绑定(bind)为每次鼠标进入时检查“if”语句的事件.input-medium 的区域,但“if”仅检查 1 次。就不会有类(Class)和“如果”变为 false - 悬停仍然有效。

我每次都需要检查类是否存在。

$('.input-medium').on('mouseover' , function() {
if ($(this).attr('id') === 'error-highlight') {
$(this).hover(
function() {
$('<p class="reg-tooltip">test test</p>').appendTo('body');
},

function() {
$('p').remove();
});
}
});

最佳答案

解决方案:将 if 条件添加到 hover 本身:

$('.input-medium').hover(
function() {
if (this.id === 'error-highlight') {
$('<p class="reg-tooltip">test test</p>').appendTo('body');
}
},
function() {
$('p.reg-tooltip').remove(); // <-- Always remove temporary element
}
);

关于javascript - 悬停和 if 语句仅触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9252335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com