2010-09-23 30 views
1

原諒代碼大於必要的,將在適當的時候整理它。jquery動作發射一次,但不是再次

一切似乎都在工作,然而,當你點擊一個鏈接後,它的內容已經'積極'沒有任何反應。

我確定這是簡單的東西,但我看不到它。

編輯:下面的代碼現在可以在FF和Chrome中使用,但在IE8中不起作用。有任何想法嗎?

$(function(){ 
      //initialize active link to not have a link on ready. 
      var linkId = $('#pricing_body div.active').attr('id'); 
      var activeLink = $('#pricing_nav ul li#'+linkId+' a'); //just the link itself. 
      activeLink.parent().addClass('activeSection'); 
      //var activeLinkContents = activeLink.parent().html(); //the link contained in the the list item and it's contents. 
      //alert(activeLinkContents); 
      var linkContents = activeLink.html(); //text content of the link. 
      activeLink.parent().html(linkContents); 

      //when link is clicked, store it's text for assignment after <a> is stripped out. 
      $('#pricing_nav ul li a').live('click',function(){ 
       var clickedId = $(this).parent().attr('id'); 
       var clickedLinkContents = $(this).html(); 
       $(this).parent().addClass('activeSection'); 
       $(this).parent().html(clickedLinkContents); //replaces <a><span>name</span></a> with just the span and text. 

       //fadeOut active div and give it inactive class. get list item with same id as div we are fading out. 
       $('#pricing_body div.active').fadeOut('500',function(){ 
        $(this).removeClass('active').addClass('inactive'); 
        var divId = $(this).attr('id'); 
        var sisterLink = $('#pricing_nav ul li#'+divId); 
        sisterLink.removeClass('activeSection'); 
        sisterLink.html('<a href="#">'+sisterLink.html()+'</a>'); //put link in between <li>. 

        //fadeIn the div with id of the link that has been clicked. 
        $('#pricing_body div#'+clickedId).fadeIn('500',function(){ 
         $(this).addClass('active').removeClass('inactive'); 
         var newActive = $('#pricing_nav ul li#'+clickedId); 
        }); 
       }); 
      }); 
     }); 
+0

這是其他問題愚弄的人,基本上你需要重新創建元素後重新綁定或使用.live() – 2010-09-23 19:50:14

+0

或使用委託() – mkoistinen 2010-09-23 19:56:40

+0

非常感謝。我選擇了.live()。我正確地思考.live()將事件綁定到元素的所有未來化身,但.bind()僅在頁面加載時執行一次,實際上與.click()相同? – 2010-09-23 20:49:49

回答

1

使用live方法將事件附加到元素。 Here是文檔。

0

嘗試:

$('#pricing_nav ul li a').live('click', function(){ 

--------- 
--------- 
--------- 

}); 

編輯:

在評論的答覆。

.live()

的.live()方法能夠影響尚未通過使用事件 委派的加入 到DOM 元素:綁定到一個 祖先元素的處理程序是負責 後代觸發的事件。

.bind()

的.bind()方法是附着行爲到 文檔的主 裝置。所有JavaScript事件類型, (如焦點,鼠標懸停和調整大小), 都允許用於eventType。

Here is SO Question on this difference.

+0

謝謝。我用.live而不是.click,現在它工作。我也測試了.bind,但這不起作用。 live()將事件綁定到元素(即使它們被重新創建),而bind()只是分配一次事件? – 2010-09-23 20:47:23

相關問題