我正在使用 http://sharethis.com/按钮,为网站上的用户提供一种共享内容的简单方式。
他们在这里有关于“自定义按钮”的信息:http://help.sharethis.com/customization/custom-buttons
它看起来像指定 URL,在示例中它们只有这样的 html:
<span class="st_sharethis" st_url="http://mycustomurl.com" st_title="Sharing Rocks!"
displayText="ShareThis"></span>
但我需要能够在不重新加载页面的情况下即时动态创建按钮。
因此,在我正在构建的 Flash 应用程序中,我有一个“共享按钮”,它在网页上触发一个 javascript 函数,使“弹出”div 淡入屏幕中央,我想显示共享按钮.根据有人点击的分享按钮,它需要有一个动态创建的 URL。
如果他们关闭对话框,然后再次单击共享按钮,这很重要,我希望代码会用新按钮和新 url/标题覆盖旧代码。
因此,我创建了这个从 Flash 调用的 javascript 函数,同时从 Flash 传递 URL。我将脚本和所有内容放在我的“弹出”div 中,希望按钮将在该 div 中呈现。
function shareChannel(theurl)
{
//I cant seem to find an example of what to put here to load the buttons, and give them a custom URL and title
//finally display the popup after the buttons are made and setup.
displaypopup();
}
任何人都可以发布一个示例,说明我如何在不重新加载页面的情况下执行此操作吗?
编辑: @立方体
<script type="text/javascript">
function shareChannel(chaan)
{
document.getElementById('spID1').setAttribute('st_url', 'http://mycustomurl?chan='+chaan);
document.getElementById('spID1').setAttribute('st_title', 'Custom Title is ['+chann+'] it works!');
stButtons.locateElements();
centerPopupThree();
loadPopupThree();
}
</script>
基本上什么都没发生。我讨厌使用 javascript,因为我真的不知道如何获取错误消息或调试。当我使用 flex 时,我有一个很棒的调试器/控制台可以随意使用。所以,抱歉,我无法提供更多信息。当我只删除
时,我确实注意到了document.getElementById('spID1').setAttribute('st_url', 'http://mycustomurl?chan='+chaan);
document.getElementById('spID1').setAttribute('st_title', 'Custom Title is ['+chann+'] it works!');
出现弹出窗口。
但是如果我在函数中有上面的 setAttribute 代码,弹出窗口永远不会出现,所以我假设该代码中的某些内容破坏了它。有什么建议么?或者至少我可以使用 chrome 告诉我错误是什么?我尝试了 inspect element 控制台,但当我触发该功能时,那里什么也没有出现。
此外,在我的代码顶部,他们让我使用:
<script type="text/javascript">var switchTo5x=true;</script>
<script type="text/javascript" src="http://w.sharethis.com/button/buttons.js">
</script><script type="text/javascript">stLight.options({publisher:'mypubidgoeshere'});</script>
我应该使用
stLight.locateElements();
而不是 stButtons.locateElements();
最佳答案
我一直在用
stButtons.locateElements();
通过 AJAX 加载内容后生成任何新按钮的功能,我想它也适用于此。
这个问题也可能有一些帮助:
Sharethis button does not work on pages loaded via ajax
更好的解决方案:
让你的<div>
并确保所有 <span>
s 有 ID,否则你可以给它们类,但你需要遍历它们。
function shareChannel(theurl, thetitle)
{
document.getElementById('spanID1').setAttribute('st_url', theurl);
document.getElementById('spanID1').setAttribute('st_title', thetitle);
stButtons.locateElements();
//finally display the popup after the buttons are made and setup.
displaypopup();
}
关于javascript - 如何使用带有 javascript 函数的自定义 URL 动态创建 "Share This buttons"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7289021/