javascript - 使用 jQuery 更新 sharethis chicklet 按钮的 st_url 属性?

标签 javascript jquery sharethis

我正在将所有 sharethis 跨度的属性“st_url”动态更新为使用 jQuery 单击的视频的 url。在 Firebug 中,您可以看到它正在更新我的 span 的 st_url 属性,但是我的 sharethis 按钮仍然绑定(bind)到初始 url。我读到我可能必须重新初始化这些元素,但我不确定最好的方法是什么?有没有人这样做过或知道使用更新的 url 重新初始化按钮的最佳方法?谢谢!

Sharethis includes和init:

<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:'xxxx'});
</script>

我的标记:

<span st_url="http://sharethis.com" class='st_stumbleupon' ></span>
<span st_url="example" class='st_facebook' ></span>
<span st_url="example" class='st_twitter' ></span>
<span st_url="example" class='st_email' ></span>

我的 jQuery 在用户点击视频时更新 attr="st_url":

//note: this is triggered by .bind('click')
var youtubeID = $(this).attr('id');
$('#container div > span').each(function(){
    //update sharethis to current video
    $(this).attr('st_url','http://www.youtube.com/watch?v=' + youtubeID);
});

最佳答案

这是我项目中的代码。希望,您可以使用它。

// define attributes for buttons
settings.share_buttons = [
    ['facebook','st_facebook_large','Facebook','Share on Facebook'],
    ['twitter','st_twitter_large','Tweet','Share on Twitter'],
    ['pinterest','st_pinterest_large','Pinterest','Share on Pinterest'],
    ['linkedin','st_linkedin_large','LinkedIn','Share on LinkedIn'],
    ['googleplus','st_googleplus_large','Google +','Share on Google +'] 
]; //[service,class,displayText,title]

// all code under could be placed into a function and bind on a click event 
// (don't forget about variable with URL, 'our_link_to_share').

//if block with buttons already exists
if (document.getElementById('share_this_id')){
    $('#share_this_id' > span').each(function(){
     $(this).removeAttr('st_url').attr('st_url',our_link_to_share).empty();
        stWidget.addEntry({
            "service":$(this).prop('service'),
            "element":this,
            "url":$(direct_link).prop('value'),
            "title":$(this).prop('title'),
            "type":"large",
            "text":$(this).prop('displayText'),
            "image":"http://www.softicons.com/download/internet-icons/social-superheros-icons-by-iconshock/png/256/sharethis_hulk.png",
            "summary":"My site"
        });
    });
}// if not, we'll create it
else{
    $('body').append($('<div>').prop('id', share_this_id).prop('class', 'share_this'));
    var share_this = document.getElementById('share_this_id');
    for(i=0; i&ltsettings.share_buttons.length; i++){
        $(share_this).append($('<span>').attr('st_url', our_link_to_share).prop('class', settings.share_buttons[i][1]).prop('displayText', settings.share_buttons[i][2]).prop('title', settings.share_buttons[i][3]).prop('service', settings.share_buttons[i][0]));
    }
}
if (stButtons){stButtons.locateElements();},

关于javascript - 使用 jQuery 更新 sharethis chicklet 按钮的 st_url 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5957671/

相关文章:

javascript - 动态加载js时,为什么保持脚本类型="text/javascript"

javascript - 悬停弹出菜单不适用于导航子链接

javascript - 在页脚加载延迟加载的button.js

javascript - 在 Sharethis 上的电子邮件中添加主题?

sharethis - 带有 ShareThis 的动态 URL

javascript - 设置单元格值并使其在内联编辑中保持可编辑

javascript - Redux 连接的 React 应用程序 - 在我的副作用完成之前如何不渲染它?

jquery - Typed.js 限制第一个元素退格

javascript - 如何使用 Backbone.js 在 Bootstrap Modal 中加载 API 结果

javascript - 在 jquery 中的标签内附加标签