2012-07-17 53 views
0

在每次成功的http請求後,我都會在新按鈕中使用ajaxing。如何通過Ajax使用sharethis

//Remove events and items 
$('#sthoverbuttons-chicklets span').remove(); 

然後我通過將一個對象傳遞給stwidget來添加新的事件。

看到這裏文檔sharethis:http://support.sharethis.com/customer/portal/articles/475079-share-properties-and-sharing-custom-information#Dynamic_Specification_through_JavaScript

//Finish with share buttons 
wyrAjax.sharethis.finishAddingShareButton(); 

wyrAjax.sharethis = { 
//Grab the current share height so we can keep it this height when we remove all the items 
    shareThisHeight:null, 
    init:function() { 

     //If the height has not been set set it 
     if(wyrAjax.sharethis.shareThisHeight===null){ 
      wyrAjax.sharethis.shareThisHeight = $('#sthoverbuttons').outerHeight(); 
     } 

     //Set up elements so that we can use them as ID's 
     $('.sthoverbuttons-chicklets').attr('id', 'sthoverbuttons-chicklets'); 
     if (!$('#shareLoading').length) { 
      $('#sthoverbuttonsMain').append('<div id="shareLoading"><img src="/img/loading.gif" style="position: absolute; top: 50%;left: 37%"></div>'); 
     } 
    }, 
    shareTypes:function(){ 
     var array = []; 
     array[0]={ 
      "service":"facebook" 
     }; 
     array[1]={ 
      "service":"twitter" 
     }; 
     array[2]={ 
      "service":"delicious" 
     }; 
     array[3]={ 
      "service":"googleplus" 
     }; 
     array[4]={ 
      "service":"reddit" 
     }; 
     array[5]={ 
      "service":"tumblr" 
     }; 
     array[6]={ 
      "service":"stumbleupon" 
     }; 
     array[7]={ 
      "service":"digg" 
     }; 
     array[8]={ 
      "service":"sharethis" 
     }; 
     return array; 
    }, 
    startGettingShareButton:function() { 
     //First we run a quick check to see if the elemnts have ID's 
     wyrAjax.sharethis.init(); 

     //Now lets fade out and clean up all the shares so we can add new shares in. 
     $('#sthoverbuttons-chicklets').hide(); 

     $('#sthoverbuttonsMain').height(wyrAjax.sharethis.shareThisHeight); 
     wyrAjax.sharethis.addLoadingToShare(); 
    }, 
    addLoadingToShare:function() { 
     $('#shareLoading').show(); 
     $('#sthoverbuttons-chicklets span').off().remove(); 
    }, 
    finishAddingShareButton:function() { 
     $('#shareLoading').hide(); 
     var shareItems = wyrAjax.sharethis.shareTypes(); 
     $.each(shareItems,function(key, value){ 
      wyrAjax.sharethis.addShareThisButton(value); 
     }); 
     $('.sthoverbuttons-chicklets').show(); 
    }, 
    addShareThisButton:function (object) { 
     stWidget.addEntry({ 
      "service":object.service, 
      "element":document.getElementById('sthoverbuttons-chicklets'), 
      "url":"http://www.wouldyourathers.co.uk/question/" + wyrAjax.questionDetails.id, 
      "title":"Would You Rather | " + wyrAjax.questionDetails.q1, 
      "type":"large", 
      "text":"Would You Rather " + wyrAjax.questionDetails.q1 + " or " + wyrAjax.questionDetails.q2, 
      "summary":wyrAjax.questionDetails.q1 + " or " + wyrAjax.questionDetails.q2 
     }); 
    } 
}; 

當我點擊新添加的按鈕,它會去,例如Twitter的的分享功能之一,但它也帶來了一些原因,Facebook的份額。

我相信我想在刪除所有事件之前重新添加它們。

+0

'.remove()'除了元素外還會刪除所有事件,所以問題可能在其他地方。您應該提供更多的腳本,以便我們更好地瞭解正在發生的事情。 – MrOBrian 2012-07-17 22:59:30

+0

我認爲這一點,但我認爲我錯了,因爲他們沒有被刪除。你好,我會更新這個問題。只是不想在這裏投入太多,不想嚇跑別人 – 2012-07-17 23:01:34

回答

0

所以我遇到的問題是sharethis不希望你刪除共享項目。 Sharethis stWidget.addEntry不希望您添加新的dom項目,它希望您替換頁面上已有的當前共享按鈕。

考慮到這一點我修改了代碼的ID添加到已共享的所有按鈕:

//Give all share buttons ID's 
var shareItems = wyrAjax.sharethis.shareTypes(); 
$.each(shareItems,function(key, value){ 
    $('.st_'+value.service+'_large').attr('id','st_'+value.service+'_large'); 
}); 

然後我用Sharethis的addEntry只需更換現有按鈕。

wyrAjax.sharethis = { 
    //Grab the current share height so we can keep it this height when we remove all the items 
    shareThisHeight:null, 
    init:function() { 

     //If the height has not been set set it 
     if(wyrAjax.sharethis.shareThisHeight===null){ 
      wyrAjax.sharethis.shareThisHeight = $('#sthoverbuttons').outerHeight(); 
     } 

     //Set up elements so that we can use them as ID's 
     $('.sthoverbuttons-chicklets').attr('id', 'sthoverbuttons-chicklets'); 
     if (!$('#shareLoading').length) { 

      //Give all share buttons ID's 
      var shareItems = wyrAjax.sharethis.shareTypes(); 
      $.each(shareItems,function(key, value){ 
       $('.st_'+value.service+'_large').attr('id','st_'+value.service+'_large'); 
      }); 

      $('#sthoverbuttonsMain').append('<div id="shareLoading"><img src="/img/loading.gif" style="position: absolute; top: 50%;left: 37%"></div>'); 
     } 
    }, 
    shareTypes:function(){ 
     var array = []; 
     array[0]={ 
      "service":"facebook" 
     }; 
     array[1]={ 
      "service":"twitter" 
     }; 
     array[2]={ 
      "service":"delicious" 
     }; 
     array[3]={ 
      "service":"googleplus" 
     }; 
     array[4]={ 
      "service":"reddit" 
     }; 
     array[5]={ 
      "service":"tumblr" 
     }; 
     array[6]={ 
      "service":"stumbleupon" 
     }; 
     array[7]={ 
      "service":"digg" 
     }; 
     array[8]={ 
      "service":"sharethis" 
     }; 
     return array; 
    }, 
    startGettingShareButton:function() { 
     //First we run a quick check to see if the elemnts have ID's 
     wyrAjax.sharethis.init(); 

     //Now lets fade out and clean up all the shares so we can add new shares in. 
     $('#sthoverbuttons-chicklets').hide(); 

     $('#sthoverbuttonsMain').height(wyrAjax.sharethis.shareThisHeight); 
     wyrAjax.sharethis.addLoadingToShare(); 
    }, 
    addLoadingToShare:function() { 
     $('#shareLoading').show(); 
    }, 
    finishAddingShareButton:function() { 
     //Remove the loading.gif 
     $('#shareLoading').hide(); 

     //grab array of different share types 
     var shareItems = wyrAjax.sharethis.shareTypes(); 

     //Loop through 
     $.each(shareItems,function(key, value){ 
      wyrAjax.sharethis.addShareThisButton(value); 
      $('#st_'+value.service+'_large > span:first-child').remove(); 
     }); 
     $('.sthoverbuttons-chicklets').show(); 
    }, 
    addShareThisButton:function (object) { 
     stWidget.addEntry({ 
      "service":object.service, 
      "element":document.getElementById('st_'+object.service+'_large'), 
      "url":"http://www.wouldyourathers.co.uk/question/" + wyrAjax.questionDetails.id, 
      "title":"Would You Rather | " + wyrAjax.questionDetails.q1, 
      "type":"large", 
      "text":"Would You Rather " + wyrAjax.questionDetails.q1 + " or " + wyrAjax.questionDetails.q2, 
      "summary":wyrAjax.questionDetails.q1 + " or " + wyrAjax.questionDetails.q2 
     }); 
    } 
}; 

希望有人能找到這個幫助。