使用jQuery更新sharethis参数这不起作用?

时间:2016-08-18 17:30:07

标签: javascript jquery html sharethis

我在过去3天内修复了ShareThis按钮,当div的内容发生变化时,该按钮实际上并没有更新参数(例如title,summary,url)。

当窗口加载时它工作正常但是一旦div的文本(在我的情况下文本是引号和作者)被更改,则sharethis参数不会更新以匹配当前文本。

这是我首先执行的.ready()函数,



$(document).ready(function() {
	$(function() {
    randomQuote();	// Generates random quotes
	});
    // Setting parameters of ShareThis
    $(window).load(function(){ // Updates the sharethis quotes and author as soon as page is finished loading and quotes and authors are displayed
   		 stWidget.addEntry({
             "service":   "sharethis",
             "element":   document.getElementById('shareThisCustom'),
             "url":       window.location.href,
             "title":     "Quote by: " +$("#reference").text(),
             "type":      "large",
             "text":      "QuotesBits" ,
             "image":     "http://abdurrehman.pk.tn/QuotesBits/images/qoutes-bits-logo.jpg",
             "summary":   $("#quotes").text()
    		 });
   		updateMeta(); // Updates <meta> elements
	});

    // facebook SDK
    window.fbAsyncInit = function() {
    	FB.init({
     	appId      : '231016417299211',
      	xfbml      : true,
      	version    : 'v2.7'
    	});
	};

     (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "https://connect.facebook.net/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   	 }(document, 'script', 'facebook-jssdk'));
     // End of facebook SDK
});
&#13;
&#13;
&#13;

当页面加载并显示引号时,它可以更新sharethis参数,但只要更改引号,其参数保持不变。

只要更改报价,就会调用以下函数:

&#13;
&#13;
var updateMeta = function(){
	// Setting up meta tags for social share links
	$('.meta-title').attr('content', "Quote by: " +$("#reference").text());
	$('.meta-url').attr('content', window.location.href);
	$('.meta-desc').attr('content', $("#quotes").text());

    /*
    var switchTo5x = true;
	$.ajax({  // Reloads the script whenever a quote and author is changed
	    url: 'http://w.sharethis.com/button/buttons.js',  
	    dataType: 'script',  
	    success: function(){  
	    // Setting stLight options
	    stLight.options({publisher: "4d3ed2be-9bb6-4996-bd76-c88508432041", onhover: false, doNotHash: false, doNotCopy: false, hashAddressBar: false})  
	    } 
	});  
    */
	// Again updating the parameters of sharethis
	stWidget.addEntry({
         "service":   "sharethis",
         "element":   document.getElementById('shareThisCustom'),
         "url":       window.location.href,
         "title":     "Quote by: " +$("#reference").text(),
         "type":      "large",
         "text":      "QuotesBits" ,
         "image":     "http://abdurrehman.pk.tn/QuotesBits/images/qoutes-bits-logo.jpg",
         "summary":   $("#quotes").text()
	});

stButtons.locateElements();
}
&#13;
&#13;
&#13;

现在的问题是,如果我重新加载sharethis脚本,它会中断按钮,这意味着在重新加载sharethis脚本后,每当我点击sharehtis按钮时,没有任何反应(这就是为什么我把.ajax函数推入了注释)。

最后,这是我的共享元素:

&#13;
&#13;
<span class="social-image-span">
  	<a class="sharethis" href="javascript:void(0);"> <img class="sharethis" src="images/icons-normal/Share-hover.png" alt="share" /> </a>
  	<a href="javascript:void(0);"> <img id="shareThisCustom" class="sharethis st_sharethis_large fade" src="images/icons-normal/Share.jpg" alt="share" /> </a>
</span>
&#13;
&#13;
&#13;

这个问题还有其他方法吗?

P.S:您可以查看我的项目现场演示,以便更好地理解:QuotesBits

任何帮助都会受到极大关注。

0 个答案:

没有答案