2012-04-12 4 views
0

나는이 드롭 다운 메뉴를 조작하기 위해 현재이 스크립트를 사용하고 있습니다. 사용자가 메뉴 항목을 클릭하면 드롭이 나타나고 현재 스크린 바깥 쪽을 클릭하여 메뉴가 사라졌지만 id가 항목을 바꿀 수있는 옵션을 유지하면서 메뉴 항목을 다시 클릭하여 드롭 다운을 사라지게하는 기능을 추가하는 방법에 대해 궁금합니다. 구현하기가 어렵습니까? 메신저 오류를 받고 있지만클릭시 jquery 드롭 다운 스크립트에 hide 함수 추가하기

/* function to show menu when clicked */ 
dropdown.bind('click',function(e) { 
    if (!dropdown.data('open')) { 
    dropdown.data'(open', true); 
    // open menu 
    } else { 
    dropdown.data('open', false); 
    // close menu 
    } 

}); 

내가 살아야 잘못 혹은 덮어 뭔가를 연주 가정 있도록 :

여기
$(document).ready(function() { 
    /* for keeping track of what's "open" */ 
    var activeClass = 'dropdown-active', showingDropdown, showingMenu, showingParent; 
    /* hides the current menu */ 
    var hideMenu = function() { 
     if(showingDropdown) { 
      showingDropdown.removeClass(activeClass); 
      showingMenu.hide(); 
     } 
    }; 

    /* recurse through dropdown menus */ 
    $('.dropdown').each(function() { 
     /* track elements: menu, parent */ 
     var dropdown = $(this); 
     var menu = dropdown.next('div.dropdown-menu'), parent = dropdown.parent(); 
     /* function that shows THIS menu */ 
     var showMenu = function() { 
      hideMenu(); 
      showingDropdown = dropdown.addClass('dropdown-active'); 
      showingMenu = menu.show(); 
      showingParent = parent; 
     }; 
     /* function to show menu when clicked */ 
     dropdown.bind('click',function(e) { 
      if(e) e.stopPropagation(); 
      if(e) e.preventDefault(); 
      showMenu(); 
     }); 
     /* function to show menu when someone tabs to the box */ 
     dropdown.bind('focus',function() { 
      showMenu(); 
     }); 
    }); 

    /* hide when clicked outside */ 
    $(document.body).bind('click',function(e) { 
     if(showingParent) { 
      var parentElement = showingParent[0]; 
      if(!$.contains(parentElement,e.target) || !parentElement == e.target) { 
       hideMenu(); 
      } 
     } 
    }); 
}); 

는 아래의 대답에 내 시도를 기반으로? 이것은이 코드로 편집 된 유일한 섹션이므로 위 코드의 섹션 일뿐입니다.

감사

답변

0

은 당신이 할 수있는 것은 jQuery.data을 (사용) 및 변수 상태에 따라 true 또는 false 예를 오픈라는이 있습니다. 그래서 너는 이렇게 될 수있다.

if (!dropdown.data('open')) { 
dropdown.data'(open', true); 
// open menu 
} else { 
dropdown.data('open', false); 
// close menu 
} 
+0

대략 어디에 배치할까요? 클릭 기능에 포함 되었습니까? – Doidgey

+0

예, 내부에서 이벤트 처리기/기능을 클릭하십시오. 처음에는 그것을 취소하지 않으므로 열어서 사실이라고 선언하므로 두 번째로 닫고 다시 거짓으로 설정할 수 있습니다. – GillesC

+0

나는 나의 시도를 포함하기 위해 질문을 편집하려고한다. 당신은 단지 그것을 조사 할 수있다, 미안하다. jquery suck의 나의 기술에 고통이된다. – Doidgey