18

我正在尝试在通过 ajax 加载的页面上使用 sharethis 按钮。按钮不显示。请帮忙。

问候, 潘卡伊

4

10 回答 10

44

将新内容添加到 dom 后,调用

stButtons.locateElements();

// or if you want to be a bit defensive about whether the lib has been
// loaded or not:
if (window.stButtons){stButtons.locateElements();} // Parse ShareThis markup

文章 另一个 另一个

于 2011-06-21T22:40:49.263 回答
20

更新 09/2017 答案

stButtons 对象不再存在,现在您可以使用

window.__sharethis__.initialize()

重新初始化按钮

于 2017-08-30T10:30:26.110 回答
4

2020 年 3 月更新答案

如上面 The Ape 的回答所示:https ://stackoverflow.com/a/45958039/1172189

window.__sharethis__.initialize()

这仍然有效,但是如果您的 URL 在 AJAX 请求上发生更改,则需要确保将要共享的 URL 设置为data-url内联共享 div 上的数据属性 ( )。您还可以使用data-title属性更新标题。

<div class="sharethis-inline-share-buttons" data-url="http://sharethis.com" data-title="Sharing is great!"></div>

用例:

我正在使用 WordPress/PHP 函数根据查询字符串生成特定内容。如果您没有data-url在 ShareThis div 上设置属性,ShareThis 将保存第一个被点击分享的 URL,而不管 AJAX 请求上的 URL 更新。

于 2020-03-06T17:24:05.200 回答
3

该解决方案也适用于基于 NodeJS 的框架,例如 Meteor。

stButtons.locateElements();

在模板的渲染回调中需要,以确保 shareThis 按钮将出现在页面重定向上。

于 2015-02-16T19:50:34.567 回答
2

我在使用 sharethis 和 Ajax 分页时遇到了同样的问题。在 Ajax 加载帖子后,按钮没有显示,所以我搜索并找到了这个。我刚刚stButtons.locateElements();在 Ajax 上添加了该功能success:

就像是success: stButtons.locateElements();

希望这对像我这样的人有所帮助。

谢谢伊布努尔

于 2015-01-14T18:19:34.423 回答
1

对于新的 API,以下解决方案对我有用

if (__sharethis__ && __sharethis__.config) {
    __sharethis__.init(__sharethis__.config);
}

加载 ajax 内容后添加此代码。

于 2017-12-22T12:44:23.607 回答
1

做这个:

window.__sharethis__.load('inline-share-buttons', config);

并使用 javascript 配置您的按钮。

于 2018-09-12T20:02:04.047 回答
0

以下内容应适用于当前的 ShareThis javascript。如果 sharethis js 没有加载,脚本会加载它。如果它已经加载,ShareThis 将使用当前 URL 重新初始化,以便它在通过 Ajax 加载的页面上工作。mounted与Vue 组件上的方法一起使用时,对我来说工作正常。

const st = window.__sharethis__
if (!st) {
  const script = document.createElement('script')
  script.src =
    'https://platform-api.sharethis.com/js/sharethis.js#property=<your_property_id>&product=sop'
  document.body.appendChild(script)
} else if (typeof st.initialize === 'function') {
  st.href = window.location.href
  st.initialize()
}

确保在脚本 src url 中使用 ShareThis 提供的属性 ID。

于 2020-10-30T05:37:14.417 回答
0

在 Drupal 中,您可以通过添加以下代码来实现此目的:

(function($){
  Drupal.behaviors.osShareThis = {
    attach: function(context, settings) {
      stLight.options({
        publisher: settings.publisherId
      });
      // In case we're being attached after new content was placed via Ajax,
      // force ShareThis to create the new buttons.
      stButtons.locateElements();
    }
  };
});
于 2017-03-22T08:40:40.987 回答
-3

我在其中一个 addThis 论坛上找到了以下解决方案,它对我很有用。我调用该函数作为对我的 ajax 调用的回调。希望这有帮助

<script type="text/javascript">
       function ReinitializeAddThis(){
      if (window.addthis){
         window.addthis.ost = 0;
         window.addthis.ready();
      }
   }
...
$('#camps-slide .results').load(loc+suffix, function() {ReinitializeAddThis();});
</script>
于 2012-01-26T22:28:20.463 回答