2013-07-28 8 views
0

스크립트에 문제가 있습니다. 계속하지 않습니다. "링크 5"를 클릭하면 창이 나타납니다. 그런 다음이 창을 닫고 "닫기"버튼을 누르십시오. 다른 함수, 즉 onkeypress 함수가 필요합니다. Esc 키를 누르면 창을 닫아야합니다. 네가 나를 도울 수 있기를 바랍니다.자바 스크립트 Onclick 및 Onkeypress

<li onClick="return pop('pop')" id="stream">Link 5</li> 
     <div id="pop" class="parentDisable" onselectstart="return false" onselectstart="return false"> 
      <table border="1" id="popup"> 
       <tr> 
        <td> 
         <a href="" onClick="return hide('pop')" style="float: right; margin: 4px;"> 
          <img src="http://www.imgbox.de/users/Metraax/close.png" /> 
         </a> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <h2>Fenster ge&ouml;ffnet</h2> 
        </td> 
       </tr> 
       <tr> 
        <td>height: auto;</td> 
       </tr> 
      </table> 
     </div> 
     <script type="text/javascript"> 
     function pop(div) 
     { 
     document.getElementById(div).style.display='block'; 
     return false 
     } 
     function hide(div) 
     { 
          if (e.keycode == '27') 
        {document.getElementById(div).style.display='none';} 
     document.getElementById(div).style.display='none'; 
     document.getElementById(div).style.display='none'; 
     return false 
     } 
     </script> 

    <style> 

    .parentDisable { 
     z-index:999; 
     width:100%; 
     height:100%; 
     display:none; 
     position: absolute; 
     top:0; 
     left:0; 
     background-color: rgba(204, 204, 204, 0.4); 
     color: #aaa; 
     filter: alpha(opacity=50); 
    } 
    #popup { 
     width: 44.48%; 
     position: absolute; 
     top: 200px; 
     left: 27.76%; 
     color: #000; 
     background-color: #C4C4C4; 
     border-radius: 5px; 
     box-shadow: 0px 0px 20px gray; 
    } 
    #popup tr td h2 { 
     float: left; 
     font-size: 20px; 
    } 
    #popup tr { 
     cursor: default; 
    } 
    </style> 
+1

이 이전 질문에 도움이 될 수 있습니다. http://stackoverflow.com/questions/1160008/which-keycode-for-escape-key-with-jquery – Sid

+0

UL이없는 LI 요소가 있고 형제로 DIV가있는 이유는 무엇입니까? 권리 ? – adeneo

+0

어 - 레이아웃 테이블 –

답변

1

뭔가 :

function pop(div) { 
    var d = document.getElementById(div); 
    d.style.display = 'block'; 

    if (document.addEventListener) { 
     document.addEventListener ("keyup", function(e) { 
      onEsc(e, d); 
     }, false); 
    }else{ 
     if (document.attachEvent) 
      button.attachEvent ("keyup", function(e) { 
       onEsc(e, d); 
      }); 
    } 
    return false 
} 

function hide(div) { 
    document.getElementById(div).style.display = 'none'; 
    return false 
} 

function onEsc(event, elem) { 
    if (event.which == null && (event.charCode != null || event.keyCode != null)) { 
     event.which = event.charCode != null ? event.charCode : event.keyCode; 
    } 
    if (event.which === 27) { 
     elem.style.display = 'none'; 
     document.removeEventListener("keyup"); 
    } 
} 

FIDDLE

+0

고맙습니다. –

+0

@Selfproblemmade - 여러분도 환영합니다. 이전 브라우저에서도 작동하도록 편집되었습니다. – adeneo

+0

자바 스크립트에 문제가 있으면 내 연락 담당자가 될 수 있습니까? –

0

당신은 jQuery를이 코드를 사용할 수 있습니다 :이 같은

$(document).keyup(function(e) {  
    if (e.keyCode == 27) { <YOUR CODE HERE> } // esc 
});