0

두 번 클릭 감지를 Ctrl- 클릭으로 바꾸려면 바꿀 수 있지만이를 수행하는 방법을 모르겠습니다.이벤트 핸들러에서 Ctrl + 클릭 감지

$(document).ready(function() { 
    $(document).dblclick(function (e) { 
     var target = (e && e.target) || (event && event.srcElement); 
     var tag = target.tagName.toLowerCase(); 
     if (!(tag == 'input' || tag == 'textarea' || tag == 'img')) {  
      openIfSelected("dblclick"); 
     } 
    }); 
}); 
+0

Google 크롬 확장 프로그램 (일반 jQuery와 비슷한 모양)이 맞습니까? 그렇다면 왜'event'를 정의하는 것으로 보이지 않을 때'(event && event.srcElement)'를 사용하고 있습니까? '(e && e.srcElement)'를 의미한다고하더라도,'event.srcElement'는 오래된 IE의 것입니다 (당신은 jQuery를 사용하고 있지 않습니다.). Google 크롬 확장 프로그램 인 경우 이전 브라우저를 처리 할 수있는 코드가 필요하지 않습니다. Chrome 확장 프로그램과 호환되거나 호환되는 여러 브라우저에서 확장 프로그램을 사용하려는 경우 다른 브라우저를 처리해야 할 수 있습니다. – Makyen

답변

1

당신은 jQuery를 .click() 방법과 MouseEvent.ctrlKey 재산을 찾고있다.

$(document).ready(function() { 
 
    $(document).click(function (e) { 
 
     if(e.ctrlKey){ 
 
      var tag = e.target.tagName.toLowerCase(); 
 
      if (!(tag == 'input' || tag == 'textarea' || tag == 'img')) {  
 
       openIfSelected("dblclick"); //Still pass "dblclick", but upon Ctrl-click 
 
      } 
 
     } 
 
    }); 
 
}); 
 

 
function openIfSelected(reason){ 
 
    //This is now only called upon Ctrl-click, but the "reason" remains "dblclick". 
 
    console.log('Called "openIfSelected" with reason: ' + reason); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div><kbd>Ctrl</kbd>-Click me</div>

참고 :

당신이 뭔가를 할 수
당신은 코드를 사용 : 작성된

이 이상하다. 아마도 window.event을 (를) 사용하려했습니다. 그러나 jQuery, which normalizes the event object that is passed to event handlers을 사용 중이므로 특별히 필요하지 않으므로 이와 같은 작업을 수행 할 필요가 없습니다. 또한 사용중인 태그는이 질문을 Google 크롬 확장 문제로 선언합니다. 그렇다면 jQuery를 사용하지 않아도되므로 이전 브라우저 (예 : IE)에서 코드를 사용할 필요가 없으므로이 작업을 수행 할 필요가 없습니다.

+0

먼저 답을 해주셔서 감사합니다. 'Ctrl'을 사용하면 'Ctrl'이 활성화 된 것처럼 느껴졌습니다. 그러나 단어는 선택되지 않습니다. 예. jQuery 코드입니다. 'content_script.js'파일에서이 코드를 편집하려고합니다. 사실 Google 크롬 브라우저에 사전 확장 프로그램을 추가 한 다음 '더블 클릭'으로 단어를 선택할 때 'Ctrl'을 추가하기로 결정했습니다. 그게 내가 'Ctrl + 더블 클릭'단어가 새 탭을 열 수있을 때 내가 tryin 뜻 ... 도와주세요. –

+0

나는 아래처럼 tryied. $ (문서) .ready (함수() {$ (문서) .click (함수 (E) { (e.ctrlKey) { VAR 대상 = e.target 경우; VAR 태그 = target.tagName.toLowerCase() { 경우 openIfSelected ("dblclick");} ((태그 == '입력'|| 태그 == '텍스트 영역'|| 태그 == 'IMG')!) }})를 }); –

0

이렇게 고정되었습니다. 큰 감사 Makyen.

$(document).ready(function() { 
    $(document).click(function (e) { 
     if(event.ctrlKey){ 
      var target = event.srcElement; 
      var tag = target.tagName.toLowerCase(); 
      if (!(tag == 'input' || tag == 'textarea' || tag == 'img')) { 
       openIfSelected("dblclick"); 
      } 
     } 
    }); 
}); 
+0

새로운 소문자에 직면했습니다. 제목, 대문자, 심지어 소문자처럼 변환 할 단어를 선택할 때 시도합니다. 'http://dictionary.cambridge.org/dictionary/english/'가 제목과 대문자로 작동하지 않기 때문입니다. 예제 # 1 : http://dictionary.cambridge.org/dictionary/english/easy 예제 # 2 : http://dictionary.cambridge.org/dictionary/english/Easy 나는 당신의 최고의 도움이 필요합니다. . –

관련 문제