简体   繁体   English

如何在另一个div上使用.click后使用.blur删除div

[英]How to remove a div using .blur after using .click on another div

I am trying to make a big search bar div disappear the second I remove my mouse away from that div (but ONLY after the big search bar is visible from using .click function on another search bar). 我试图让一个大的搜索栏div消失第二个我从鼠标移开我的鼠标(但只有在大搜索栏从另一个搜索栏上使用.click功能可见)。

Sorry if this sounds confusing but this is what I have done so far: 对不起,如果这听起来令人困惑,但这是我到目前为止所做的:

$("#bar-b").click(function() {
    $("#quickSearch").show();
});

The #bar-b div is the small main search bar that once I click on it, the #quickSearch div (the big search bar that I created) appears visible right underneath the small main search bar. #quickSearch #bar-b div是一个小的主搜索栏,一旦我点击它, #quickSearch div(我创建的大搜索栏)就会出现在小主搜索栏的正下方。 What I am trying to do is when I move my cursor into the #quickSearch div (I expect no changes to be made, so nothing to happen, which is what it has done so far) but when I move my cursor AWAY from #quickSearch div, I want #quickSearch div to disappear. 我想要做的是当我将光标移动到#quickSearch div时(我希望不会进行任何更改,所以没有任何事情发生,这是迄今为止所做的)但是当我从#quickSearch移动我的光标时div,我希望#quickSearch div消失。 If anyone can show me how to do that then I'd appreciate it thanks. 如果有人能告诉我如何做到这一点,那么我将非常感谢。

I have made a few attempts, which turned out to be incorrect, such as: 我做了一些尝试,结果证明是不正确的,例如:

$("#bar-b").click(function() {
    $("#quickSearch").show();
});

$("#quickSearch").mouseleave(function() {
    $("#quickSearch").hide();
});

HTML tags (listed underneath for each div if that helps): HTML标记(如果有帮助,在每个div下面列出):

<div id="bar-b">
    <a name="thesearchbox"></a>
    <input id="Keyword" accesskey="4" type="text" name="keyword" value="Search   Jessops - Keyword, name, catalogue no." style="color: rgb(153, 153, 153); ">
    <input id="cmdSearch" class="button" type="submit" value="">
</div>

<div id="quickSearch" style="border: 3px solid black; height: 250px; display: none;">/div>

check this jsfiddle link 检查这个jsfiddle链接

you code has a syntax errror 你的代码有一个语法错误

<div id="quickSearch" style="border: 3px solid black; height: 250px; display: none </div>

div is missing a closing tag..i have corrected it and it working div缺少一个结束标记..我已经纠正它并且它正常工作

<div id="quickSearch" style="border: 3px solid black; height: 250px; display: none;"> </div>​​​​​​​​​​​​

I think this is what you're after, if so then you really weren't far off! 我认为这就是你所追求的,如果是这样,你真的不远了! I've thrown together a jsFiddle example so you can play around, or instruct me further if I'm off your requirement and I'll try to improve it. 我把一个jsFiddle示例放在一起,这样你就可以玩,或者如果我不按你的要求进一步指导我,我会尝试改进它。

$("#bar-b").bind("click", function() {
    $("#quickSearch").show();
});

$("#quickSearch").bind("mouseout", function() {
    $(this).hide();
});
​ 

If I understand you correctly it looks like you're on the right path, you just need to make sure you only hide under the right circumstance. 如果我理解正确,看起来你正走在正确的道路上,你只需要确保你只能在正确的环境下躲藏。 The easiest way to achieve this would likely be to add a variable to store the state and set it to true in your event listener function. 实现这一目标的最简单方法可能是添加一个变量来存储状态,并在事件监听器函数中将其设置为true Something along the lines of putting openedWithClick = true in your onClick handler, possibly adding a check for mouse entered as well depending on your scenario. 在onClick处理程序中放置openedWithClick = true的行,可能还会根据您的场景添加对鼠标输入的检查。 You also likely want to use mouseout() rather than mouseleave() . 您也可能希望使用mouseout()而不是mouseleave() If this isn't your problem you may need to clarify what exactly is going on with your sample code and how it differs from what you expect to happen. 如果这不是您的问题,您可能需要澄清示例代码究竟发生了什么,以及它与您预期发生的情况有何不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM