2013-07-19 2 views
-1

아래 코드를 사용하면 버튼처럼 페이지를 사용할 수 있지만 문제는 다른 버튼을 클릭해도이 기능이 작동한다는 것입니다. document.body.onmousedown = function()본문 페이지를 버튼처럼 사용하십시오.

+0

귀하의 페이지에서 jQuery를 사용하고 있습니까? 그렇다면 ['event.stopPropagation()'] (http://api.jquery.com/event.stopPropagation/)을보십시오. – Mooseman

+1

이벤트가 중단되는 것을 중지하십시오. – j08691

+0

@Mooseman : 그래, 내가 사용 – user2599584

답변

0

의 jQuery 예 : 당신은 Mooseman 제안하지만 당신은 모두 페이지 처리기에 .stopPropagation()를 추가해야합니다 무엇을 할 수

// Stop bubbling with event.stopPropagation(); on inner buttons 
$("#selector").on('click',function(event){ 
    event.stopPropagation(); 
    // Run your code 
}); 

$("body").on('click', function(){ 
    // Run your code 
}); 
+0

다른 함수의 * all *에 대해 첫 번째를 사용하고 있습니까? – Mooseman

0

그래서 여기에 또 다른 솔루션입니다.

사용 event.target는 다음과 같이 이벤트의 소스를 감지 :

$('body').on('click',function(e){ 
    if (e.target === document.body) { 
     alert('body clicked'); 
    } 
}); 

또는 사용 여기서 nodeName :

if (e.target.nodeName.toUpperCase() === 'BODY') { 

또는 jQuery를 방법 :

if ($(e.target).is('body')) { 

Here's a fiddle.

관련 문제