如何在Ajax中使用sharethis
How to use sharethis with Ajax
每次成功的http请求后,我都会添加新的按钮。
//Remove events and items
$('#sthoverbuttons-chicklets span').remove();
然后,我通过将一个对象传递到stwidget来添加带有新事件的新项目。
请参阅此处的文档以共享此: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
});
}
};
当我点击其中一个新添加的按钮时,它会进入推特的共享功能,但出于某种原因,它也会显示Facebook的共享。
我相信我想在重新添加跨度之前删除跨度上的所有事件。
所以我遇到的问题是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
});
}
};
希望有人能发现这很有帮助。
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在php文件中获取$.post-ajax传递的值
- Replacing $ .ajax?
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- Ajax发布表单序列化,发布引号'
- 通过Ajax将JavaScript函数传递给PHP文件
- ajax请求的顺序总是不同的
- 可以't使用Polymer's的核心ajax
- Ajax Live搜索发布到Laravel视图
- Ajax聊天消息重复而不仅仅是更新
- 从控制器返回后Ajax启动事件激发
- PHP AJAX图片上传示例不上传
- 从ajax请求中获取javascript对象
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 如何在Javascript中解析AJAX数组的特定部分
- Ajax文件加载和<输入>文件加载
- 如何在Ajax中使用sharethis
- Sharethis不能用AJAX加载
- ShareThis -按钮加载后的AJAX内容不工作