简体   繁体   English

jQuery .not()问题

[英]jQuery .not() issues

The premise of what I'm trying to do is use jQuery to start a CSS transition to open and close a search box. 我要做的前提是使用jQuery启动CSS转换以打开和关闭搜索框。

User clicks magnifying glass icon, box opens, user clicks anywhere on the page but the search form, box closes. 用户单击放大镜图标,框打开,用户单击页面上的任意位置,但搜索表单框关闭。

To close, using this: 要关闭,请使用以下命令:

$('body *').not('#header-search, #header-field, #header-submit').click(function () {

And different variations of the answer found here: jQuery - Select everything except a single elements and its children? 以及在这里找到的答案的不同变化: jQuery-选择除单个元素及其子元素以外的所有内容? without success. 没有成功。

Clicking on the input#header-field always closes the box. 单击input#header字段将始终关闭该框。

Pen Here: 笔在这里:

http://codepen.io/anon/pen/RNbmwr http://codepen.io/anon/pen/RNbmwr

Thanks for reading. 谢谢阅读。

Your code is very aggressive ( it gets applied to all elements, so inner element to those in the .not() will trigger it ). 您的代码非常激进( 它将应用于所有元素,因此.not()中的内部元素将触发它 )。

It is better to delegate the closing of the box to the body ( since click events bubble up ), and manually cancel any event that occurs under the forbidden list. 最好将框的关闭委托给body因为单击事件会冒泡 ),并手动取消禁止列表下发生的任何事件。

$('body').on('click', function(){
   // code for closing box here
});

$('#header-search, #header-field, #header-submit').on('click', function(){
   return false; // stop bubbling of event
});

And since in your example the #header-field and #header-submit are descendants of header-search you only need to cancel the bubbling on that 并且由于在您的示例中, #header-field#header-submitheader-search后代,因此您只需要取消对该header-search的冒泡

$('#header-search').on('click', function(){
   return false; // stop bubbling of event
});

Demo at http://codepen.io/gpetrioli/pen/XJrwXO 演示在http://codepen.io/gpetrioli/pen/XJrwXO

Try the jQuery toggle() function: 尝试使用jQuery toggle()函数:

<script>
$( "button" ).click(function() {
  $( "p" ).toggle( "slow" );
});
</script>

Substitute the id for your magnifying glass for "button" and change the paragraph -- $("p") -- the search controls you want to show/hide. 将放大镜的ID替换为“按钮”,然后更改段落-$(“ p”)-您要显示/隐藏的搜索控件。 Toggle changes the visibility of the indicated id or class. 切换更改指示的ID或类的可见性。 If it is initially hidden, mouse click will make it visible; 如果最初是隐藏的,则单击鼠标将使其可见; if initially visible, mouse click will hide it. 如果最初可见,则单击鼠标即可将其隐藏。

Finally, change the speed of the transition if you don't want it to move "slow" 最后,如果您不希望过渡“缓慢”移动,请更改过渡的速度

A more complete explanation of toggle() is available at http://api.jquery.com/toggle/ 有关toggle()的更完整说明,请参见http://api.jquery.com/toggle/

There are many elements on the page. 页面上有很多元素。 Some of them may contain or be contained by the elements you name. 其中一些可能包含您命名的元素,也可能包含在您命名的元素中。 So they will still trigger the event. 因此他们仍然会触发事件。

Instead, bind a single event handler: 而是绑定一个事件处理程序:

$("body").click(function(evt) {

and check if you clicked on one of the elements: 并检查是否单击了以下元素之一:

    if( $(evt.target).parents("#header-search").length > 0) {

cancelling the handler if so: 如果是这样,则取消处理程序:

        return true;
    }

Perform the actual event otherwise: 否则,请执行实际事件:

    doSomething();
});

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

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