:not 不起作用

发布于 2024-10-15 09:43:08 字数 1314 浏览 2 评论 0原文

我创建了一个简单的导航栏,当您单击一个项目时,另一个项目会在其下方打开。消失我写的:

$("#container:not(#navbar)").click(function(){
 $("#mini_navbar_home").hide()
});

我想说“无论有人点击屏幕上的任何位置(导航栏除外),消失#mini_navbar_home”,但是

如果有帮助的话,点击容器上的任何地方都会隐藏它是脚本的一部分:

 var navs = new Array("#mini_navbar_home","#mini_navbar_aboutus","#mini_navbar_folan");
 var colors = new Array("#home_t","#aboutus_t","#folan_t");

 $(document).ready(function(){
  $("#home_t").click(function(){
   change_bg("#home_t")
   navbar_slide_toggle("#mini_navbar_home")
  });
        .
        .
        .
  $("#container:not(#navbar)").click(function(){
   hide_all()
   change_bg()
  });
 });

 function change_bg(div){
  for(i=0; i<colors.length; i++){
   if (colors[i] != div){
    $(colors[i]).css("backgroundColor", "#8895B7");
   }
  }

  if ($(div).css("backgroundColor") == "rgb(169, 181, 212)"){
   $(div).css("backgroundColor", "#8895B7")
  }
  else {
   $(div).css("backgroundColor", "#A9B5D4")
  }
 }
 function navbar_slide_toggle(div){
  for(i=0; i<navs.length; i++){
   if (navs[i] != div){
    $(navs[i]).hide();
   }
  }
  $(div).slideToggle(0);

 }
 function hide_all(){
  for(i=0; i<navs.length; i++){
   $(navs[i]).hide()
  }
 }

通过这样,#navabr 嵌套在 #container 中 我想我的解决方案对于大多数用户来说看起来很愚蠢:D

I've created a simple navigation bar that when you click on an item, another opens under it. to disappear the opened I wrote:

$("#container:not(#navbar)").click(function(){
 $("#mini_navbar_home").hide()
});

I wanted to say "wherever on the screen (except the navigation bar) that someone clicked, disappear the #mini_navbar_home", but clicking wherever on the container hides that

it's part of the script if helps:

 var navs = new Array("#mini_navbar_home","#mini_navbar_aboutus","#mini_navbar_folan");
 var colors = new Array("#home_t","#aboutus_t","#folan_t");

 $(document).ready(function(){
  $("#home_t").click(function(){
   change_bg("#home_t")
   navbar_slide_toggle("#mini_navbar_home")
  });
        .
        .
        .
  $("#container:not(#navbar)").click(function(){
   hide_all()
   change_bg()
  });
 });

 function change_bg(div){
  for(i=0; i<colors.length; i++){
   if (colors[i] != div){
    $(colors[i]).css("backgroundColor", "#8895B7");
   }
  }

  if ($(div).css("backgroundColor") == "rgb(169, 181, 212)"){
   $(div).css("backgroundColor", "#8895B7")
  }
  else {
   $(div).css("backgroundColor", "#A9B5D4")
  }
 }
 function navbar_slide_toggle(div){
  for(i=0; i<navs.length; i++){
   if (navs[i] != div){
    $(navs[i]).hide();
   }
  }
  $(div).slideToggle(0);

 }
 function hide_all(){
  for(i=0; i<navs.length; i++){
   $(navs[i]).hide()
  }
 }

by the way, #navabr is nested with #container
i guess my solution will look silly to most of the users :D

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

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

发布评论

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

评论(2

入画浅相思 2024-10-22 09:43:08

认为您的意思是,当点击不在 #navbar 内的 #container 时,您想要隐藏 #mini_navbar_home。这相当简单:

$('#container').click(function(e){
    var $navbar = $('#navbar');
    if (($navbar[0] !== e.target) && // if the click wasn't on navbar itself
        !$navbar.has(e.target).length // and it wasn't inside navbar
       ) {
        $("#mini_navbar_home").hide() // hide it
    }
});

stopPropagation 相比,它的优点是它允许您继续在 #navbar 内的元素上使用事件冒泡。例如,stopPropagation 会中断 $('a').live(...) 调用。

I think you're saying that you want to hide #mini_navbar_home when a click happens on #container that is not inside #navbar. This is fairly simple:

$('#container').click(function(e){
    var $navbar = $('#navbar');
    if (($navbar[0] !== e.target) && // if the click wasn't on navbar itself
        !$navbar.has(e.target).length // and it wasn't inside navbar
       ) {
        $("#mini_navbar_home").hide() // hide it
    }
});

The advantage this has over stopPropagation is that this allows you to keep using event bubbling on elements within #navbar. stopPropagation would break, for instance, $('a').live(...) calls.

审判长 2024-10-22 09:43:08

更简单的方法是捕获文档上的点击,然后防止导航栏上出现冒泡。这样,单击事件将永远不会到达文档,并且您的隐藏功能永远不会被触发。您也可以稍后扩展“黑名单”元素的列表,即使您单击迷你导航栏内的元素,此解决方案仍然有效。

var $mini_navbar_home = $("#mini_navbar_home");
$(document).click(function() { $mini_navbar_home.hide() });
$("#navbar").click(function(e) {
  e.stopPropagation();
  return false;
});

The easier way is going to be to capture clicks on the document, and then preventing bubbling on your nav bar. That way, the click event will never reach the document, and your hide function is never triggered. You can extend the list of "blacklisted" elements later, as well, and this solution still works even if you click on an element inside of your mini nav bar.

var $mini_navbar_home = $("#mini_navbar_home");
$(document).click(function() { $mini_navbar_home.hide() });
$("#navbar").click(function(e) {
  e.stopPropagation();
  return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文