jQuery:如果 $(this) 或另一个元素模糊,那么

发布于 2024-12-18 08:58:08 字数 733 浏览 5 评论 0原文

如果当前活动元素 $(this) 或另一个预定义元素(例如:div#tooltip)模糊,我想触发一个函数。但到目前为止我还没有找到如何做到这一点。我尝试过:

$(this).add('div#tooltip').live('blur', function(){
    $('div#tooltip').hide();
});

例如,假设 $(this) 是一个输入字段,预定义的第二个元素将是一个工具提示 div,并且如果其中之一模糊,我希望工具提示隐藏。

编辑: div#tooltip 包含一个锚点,如果单击它,该锚点不应使 div#tooltip 隐藏。

编辑2: 好的,这是对我的问题的更准确的解释。我有 $.fn.tooltip 函数,我将其应用于具有可变类名和 id 的各种文本输入。因此,该输入只能在函数内引用为$(this)
其次,我有工​​具提示 div,它是由函数创建的。该 div 的 ID 为 #tooltip。此工具提示/div 可以包含一些其他元素,例如锚点。
单击输入字段(此)时会自动显示工具提示。一旦关闭,即使输入字段再次获得焦点,也不会再次显示。

我想做的是:

当文本输入失去焦点时,必须删除工具提示 除非如果光标位于工具提示/div 内或单击此 div 内的元素。

有人吗?

I want to trigger a function if either the currently active element $(this) or another predefined element (e.g.: div#tooltip) blurs. However so far I've not found out how to do this. I've tried:

$(this).add('div#tooltip').live('blur', function(){
    $('div#tooltip').hide();
});

Imagine that $(this) would be an input field, for example, and the predefined second element would be a tooltip div and that I would want the tooltip to hide if one of those blurs.

EDIT:
The div#tooltip contains an anchor, which should not make the div#tooltip hide if it's being clicked.

EDIT 2:
Okay, here is a more accurate explanation of my problem. I've got the $.fn.tooltip function which I apply to various text-inputs which have variable class names and id's. Therefore, this input can only be referred to as $(this) within the function.
Secondly I have the tooltip div, which is created by the function. This div goes by the ID #tooltip. This tooltip / div can contain some other elements such as anchors.
The tooltip is shown automatically when the input-field (this) is clicked. Once it's closed it won't be shown again, even if the input-field will be focused again.

What I'm trying to do is:

The tooltip must be removed when the text-input loses it's focus
EXCEPT if the cursor is within the tooltip / div or if an element within this div is being clicked.

Anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦太阳 2024-12-25 08:58:08

像这样:http://jsfiddle.net/uu3zX/7/

HTML:

<input type="text" class="with-tooltip">
<span class="tooltip">?<a style="display:none" href="#">The tip</a></span>

JavaScript:

$('.with-tooltip').on('focus', function(){
    $(this).next().children().show();
});

$('.with-tooltip').on('blur', function(){
    $(this).next().children().hide();
});

$('.tooltip').hover(
    function(){
       $(this).children().show();
    },
    function(){
       $(this).children().hide();
    }
);

更新
添加了替代解决方案以满足使用 this 的 OP 要求

Like this: http://jsfiddle.net/uu3zX/7/

HTML:

<input type="text" class="with-tooltip">
<span class="tooltip">?<a style="display:none" href="#">The tip</a></span>

JavaScript:

$('.with-tooltip').on('focus', function(){
    $(this).next().children().show();
});

$('.with-tooltip').on('blur', function(){
    $(this).next().children().hide();
});

$('.tooltip').hover(
    function(){
       $(this).children().show();
    },
    function(){
       $(this).children().hide();
    }
);

UPDATE
Added alternative solution to fit OP requriment to use this

彻夜缠绵 2024-12-25 08:58:08

借用 IntoTheVoid 的小提琴:您应该将输入和工具提示包装在容器 div(或其他一些容器元素)中,以在一行中执行此操作:

$('.tooltip, input').on('mouseout', function(){
    $(this).parent().children('.tooltip').hide();
}).on('focus mouseover', function(){
    $(this).parent().children('.tooltip').show();
});

Borrowing from IntoTheVoid's fiddle: You should wrap the input and the tooltip in a container div (or some other container element) to do this in one line:

$('.tooltip, input').on('mouseout', function(){
    $(this).parent().children('.tooltip').hide();
}).on('focus mouseover', function(){
    $(this).parent().children('.tooltip').show();
});

http://jsfiddle.net/mblase75/uu3zX/5/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文