javascript - jQuery 问题 :not selector

标签 javascript jquery jquery-selectors

我有一个搜索图标,将鼠标悬停在该图标上时会显示一个输入文本搜索框。我使用 jQuery 向输入添加一个类,以在搜索图标悬停时显示文本框。效果很好。

下一个功能是,如果用户决定不单击搜索框和页面上的其他位置,搜索框就会消失。这也工作得很好。

唯一剩下的问题是,如果用户单击搜索框,它也会消失,这是我不希望的。

我的 HTML:

<input type="text" class="form-text text-hidden" title="Enter the terms you wish to search for." value="" size="15" id="edit-search-block-form-1" name="search_block_form" maxlength="128">

<input type="image" src="http://d3j5vwomefv46c.cloudfront.net/photos/large/802431483.png?1377197793" class="form-submit search-hover" alt="Search" id="edit-submit">

我的 jQuery:

$(".search-hover").hover(function () {
    $('.text-hidden').addClass('hover');
  });

  // Now remove the class in case of a false start or the user decides to do something else.
  $("body:not(.text-hidden)").click(function(){
      //alert('The body click is working!!!');
    $('.form-text.text-hidden').removeClass('hover');
  });

请注意,我尝试使用 :not 选择器来指示仅在单击搜索框以外的任何位置时删除 hover 类,但它不起作用。

I've got a fiddle here

最佳答案

尝试

jQuery(function(){
    $(".search-hover").hover(function () {
        $('.text-hidden').addClass('hover');
    });

    // Now remove the class in case of a false start or the user decides to do something else.
    $("body").click(function(e){
        //alert('The body click is working!!!');
        if($(e.target).closest('.text-hidden').length == 0)
            $('.form-text.text-hidden').removeClass('hover');
    });
})

演示:Fiddle

关于javascript - jQuery 问题 :not selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18419558/

相关文章:

Javascript AMCharts 通过 ID 引用图表不起作用

javascript - 如果值为假,则使用逻辑运算符返回未定义

javascript - 克隆div的特定内容并放入其他div

jquery - 是否有一个 JQuery 表达式将针对没有规定类的 div

javascript - Jquery 不支持伪 : focusable

javascript - 如何以 Angular 调用外部 jQuery 函数

javascript - 检查/记录 PhantomJS/CasperJS 使用了多少带宽

javascript - 如何使用 jQuery 以与数组相同的顺序对 html 元素进行排序?

javascript - 重定向

javascript - 带有 jquery 的 CSS3 slider 构造函数