2012-07-08 3 views
0

특정 div (#block_profile)에서 qtip2 툴팁을 실행할 때 아래 함수가 여러 번 트리거되는 문제가 있습니다. 그래서 4 번째 #block_profile을 클릭하면이 함수가 4 번 호출됩니다. 어떻게 클릭 한 정확한 div에 대해서만 실행되도록 할 수 있습니까?jquery : 함수가 여러 번 트리거 됨

// Create the tooltips only on document load 
$(document).ready(function() { 
    // Make sure to only match links to wikipedia with a rel tag 
    $('div.block_profile[rel]').each(function() { 


     // We make use of the .each() loop to gain access to each element via the "this" keyword... 
     $(this).qtip(
      { 
       content:{ 
        // Set the text to an image HTML string with the correct src URL to the loading image you want to use 
        text:'<img src="/assets/ux/modal/loading.gif" alt="Loading..." />', 
        ajax:{ 
         url:'/profiles/get_info/' + $(this).attr('rel') // Use the rel attribute of each element for the url to load 
        }, 
        title:{ 
         button:false 
        } 
       }, 
       position:{ 
        my:'top left', 
        target: 'mouse', 
        viewport:$(window), // Keep the tooltip on-screen at all times 
        adjust:{ 
         x:10, y:10 
        } 
       }, 
       hide:{ 
        fixed:false // Helps to prevent the tooltip from hiding ocassionally when tracking! 
       }, 
       style:{ 
        classes:'container ui-tooltip ui-tooltip-tip' 
       } 
      }) 
    }) 

     // Make sure it doesn't follow the link when we click it 
     .click(function (event) { 
      event.preventDefault(); 
     }); 
}); 

html로 :

<div id ="block_profile" class ="block_profile rel="1">div 1</div> 
<div id ="block_profile" class ="block_profile rel="2">div 2</div> 
<div id ="block_profile" class ="block_profile rel="3">div 3</div> 
<div id ="block_profile" class ="block_profile rel="4">div 4</div> 
<div id ="block_profile" class ="block_profile rel="5">div 5</div> 
+0

html의 요소 ID는 ** 고유 ** 여야합니다. 그것들을 고쳐라. – nbrooks

+0

'클래스'에서 큰 따옴표가 누락 된 것을 알 수 있습니까? – DPlusV

+0

잘못된 형식의 HTML은 제외하고 코드가 [잘 작동합니다] (http://jsfiddle.net/ult_combo/dcnSs/). "클릭"과 "기능"이 여러 번 발화된다는 것은 무엇을 의미합니까? 거기에는'event.preventDefault()'외에도 클릭 기능이 없습니다. 호버의 qtip은 정상적으로 작동합니다. –

답변

1

는 다음과 같은 코드를 사용한다는 :

// Create the tooltips only on document load 
$(document).ready(function() { 
    // Make sure to only match links to wikipedia with a rel tag 
    $('div.block_profile[rel]') 
     .qtip({ 
      ... 
     }) 
     .click(function (event) { 
      event.preventDefault(); 
     }); 
}); 

each 호출이 필요하지 않습니다. 예를 들어 this demo을 확인하십시오.

+0

예.하지만 작동하지 않습니다. 아마도 그 다른 코드 간섭 내 애플 리케이션에 많은 쿼리 기능과 플러그인이 있습니다. 내 코드가 좋지 않다고 생각했지만이 코드가 실제로 유효한지 확인해야합니다. thx – Rubytastic

+0

이것이 작동하지 않으면 다른 것이 잘못되었습니다. 'each' 콜은 정말로 거기에 있지 않아야합니다. 나는 당신의 HTML을 정리하는 힌트에 대한 다른 의견을 살펴 보시기 바랍니다. –

+0

Thx 나는 깨끗하게하기를 계획한다. 실제로 그 코드는 정확하지 않고 html로 내 부분에서 실패 할 것이다. – Rubytastic

관련 문제