2013-12-10 4 views
0

example4의 "Google"링크를 클릭하면 2 개의 경고 메시지가 표시됩니다. [alert3 메시지도 표시됩니다.] this.i에 대한 해결책을 제공하십시오. example4의 링크를 차별화하려고했지만 결과가 없습니다. 2 개의 경고 메시지가 나타납니다.jquery에서 링크를 구분하는 방법은 무엇입니까?

이 코드가 잘못된 것입니다. 저는 Jquery world에서 초보자입니다.

jQuery 코드 :

//Example 03 - Prevent Default Action of HTML Element 
$(document).ready(function(){ 
    $("a").click(function(e){ 
     e.preventDefault(); 
     alert("Example 3 :: The default action of anchor element has been prevented using Jquery"); 
    }); 
}); 

    //Example 04 - Is Default Action Prevented of HTML Element 
    $(document).ready(function(){ 
     $("a.checkaction").click(function(event){ 
      event.preventDefault(); 
      alert("Example4 :: Is default action prevented of anchor <a>element :: "+event.isDefaultPrevented()); 
    }); 
}); 

HTML 코드 :

<!-- Jquery example 03 - Prevent default behavior of HTML element using Jquery --> 
<h1 class="header">Example 3 - Prevent default behavior of HTML Element</h1> 
<a href="http://www.facebook.com">Facebook</a> 

<!-- Jquery example 04 - How to check whether the default action oh html is prevented or not using jquery --> 
<h1 class="header">Example 4 - How to check whether the default action oh html is prevented or not</h1> 
<a href="http://www.google.com" class="checkaction">Google</a> 

답변

0

다음을 시도해보십시오.

을 유의하시기 바랍니다, 당신은 하나 개의 $document.ready() 기능에 jQuery를 기능을 포장해야합니다. 또한 예 4를 클릭했을 때 두 개의 팝업이 나타났습니다. jQuery 선택기를 모두 anchors에 적용하도록 설정했기 때문입니다. anchorexample-3 클래스를 추가하고 js를 호출하도록 수정했습니다. 나는 또한 기본 동작을 방지하기 위해 return false;을 사용했습니다. 이 경우 클리너와 같은 작업을 수행합니다.

JQUERY :

$(document).ready(function(){ 

    //Example 03 - Prevent Default Action of HTML Element 
    $("a.example-3").click(function(){ 
     alert("Example 3 :: The default action of anchor element has been prevented using Jquery"); 

     return false; 
    }); 

    //Example 04 - Is Default Action Prevented of HTML Element 
    $("a.checkaction").click(function(){ 
     alert("Example 4 :: The default action of anchor element has been prevented using Jquery"); 

     return false; 
    )}; 
}); 

HTML :

<!-- Jquery example 03 - Prevent default behavior of HTML element using Jquery --> 
<h1 class="header">Example 3 - Prevent default behavior of HTML Element</h1> 
<a href="http://www.facebook.com" class="example-3">Facebook</a> 

<!-- Jquery example 04 - How to check whether the default action oh html is prevented or not using jquery --> 
<h1 class="header">Example 4 - How to check whether the default action oh html is prevented or not</h1> 
<a href="http://www.google.com" class="checkaction">Google</a> 
+0

이 코드는 잘 작동합니다. "false를 반환"한다는 것은 무슨 뜻입니까? – Arjunan

+0

이 인스턴스에서'return false; '는 요소의 기본 동작을 금지합니다. 양식과 jQuery를 다룰 때 주로 사용됩니다. 동일한 것은'preventDefault()'는 본질적으로 -하지만 코드는 적다. –

+1

감사합니다. Mike Barwick. – Arjunan

1

당신은에 첫 핸들러를 변경할 수 있습니다 :

$("a:not(.checkaction)").click(function(e){ 
    e.preventDefault(); 
    alert("Example 3 :: The default action of anchor element has been prevented using Jquery"); 
}); 

또는 두 핸들러 결합 할 수 있습니다 :

$("a").click(function(e) { 
    e.preventDefault(); 
    if ($(this).hasClass("checkaction")) { 
     ... 
    } else { 
     ... 
    } 
}); 
+0

이것은 분명히 * 많은 * 클리너 경로이며, 만약 내가 직접 작성한다면 그것에 대해 간 것입니다. 나는 OP에서 더 많은 관심사/질문을 피하기 위해 간단하게 대답했습니다. (그것은 분명합니다.) +1 –

0

메이트, 페이지의 모든 앵커 요소 (페이스 북 링크와 구글 링크)에 대한 클릭 기능을 후크 것입니다 예를 들어 3 . 그런 다음, 예를 들어 4는 구글 링크에 대한 또 다른 클릭 기능 후크 것입니다 ... 그래서 구글 링크는 다음과 같은 두 번

변경합니다 코드를 처리 할 것입니다 :

jQuery 코드 :

//Example 03 - Prevent Default Action of HTML Element 
$(document).ready(function(){ 
    $("a[name=fb]").click(function(e){ 
     e.preventDefault(); 
     alert("Example 3 :: The default action of anchor element has been prevented using Jquery"); 
    }); 
}); 

//Example 04 - Is Default Action Prevented of HTML Element 
$(document).ready(function(){ 
    $("a[name=gg]").click(function(event){ 
     event.preventDefault(); 
     alert("Example4 :: Is default action prevented of anchor <a>element ::  "+event.isDefaultPrevented()); 
}); 
}); 

html로 코드 :

<!-- Jquery example 03 - Prevent default behavior of HTML element using Jquery --> 
<h1 class="header">Example 3 - Prevent default behavior of HTML Element</h1> 
<a name="fb" href="http://www.facebook.com">Facebook</a> 

<!-- Jquery example 04 - How to check whether the default action oh html is prevented or not using jquery --> 
<h1 class="header">Example 4 - How to check whether the default action oh html is prevented or not</h1> 
<a name="gg" href="http://www.google.com" class="checkaction">Google</a> 

희망이 있습니다 의미

레오

+0

두 개의'$ (document) .ready()'함수가 필요 없습니다 ... –

+0

아무런 해가 없습니다. – Barmar

+0

둘 다 맞지만 Mike Barwick이 document.ready 함수 하나만 사용하면 코드를 훨씬 더 명확하고 쉽게 유지할 수 있다고 제안했습니다. 원래 게시물에서 복사하여 붙여 넣은 것 같습니다. – Leo

관련 문제