动态更改资料Tweet按钮"数据文本"内容 [英] Dynamically change Tweet Button "data-text" contents

查看:180
本文介绍了动态更改资料Tweet按钮"数据文本"内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题来非常接近我后,我:<一href=\"http://stackoverflow.com/questions/5571115/replace-an-attribute-in-the-tweet-button-with-jquery\">Replace在使用jQuery 鸣叫按钮的属性。

This question comes very closely to what I'm after: Replace an Attribute in the Tweet Button with Jquery

不过,建议的解决方案只是一次工作。也就是说,我不能在我的开关使用它的这样的语句:

However, the suggested solution works just once. That is, I cannot use it in my switch statement like this:

    switch(element.id)
    {
        case "t1":
            $(document).ready(function(){
                $('a[data-text]').each(function(){
                    $(this).attr('data-text', Text_Variant_1);
                });
                $.getScript('http://platform.twitter.com/widgets.js');
            });
            break;
        case "t2":
            $(document).ready(function(){
                $('a[data-text]').each(function(){
                    $(this).attr('data-text', Text_Variant_2);
                });
                $.getScript('http://platform.twitter.com/widgets.js');
            });
        ...
    }

会发生什么事是,数据文本的属性是根据任何情况下,首先发生并不会改变后设置。

What happens is that the data-text attribute is set according to whichever case happens first and doesn't change afterwards.

我怎样才能改变的数据文本的鸣叫按钮的属性很多次,因为我需要?

How can I change data-text attribute of a Tweet Button as many times as I need?

更新:这里是我工作的页面: http://zhilkin.com/socio/ EN /

Update: here's the page I'm working on: http://zhilkin.com/socio/en/

性状的表可以被忽略。我想用的 Sociotypes 的表是当您点击右边的描述下分享Tweet按钮的类型,数据文本的应相应地更改做

The Traits table can be safely ignored. What I want to do with the Sociotypes table is that when you click on a type, the data-text of the Tweet Button below the description on the right should be changed accordingly.

现在,它的工作原理是这样的:如果我将鼠标悬停或点击堂吉诃德,然后的数据文本的是要......堂吉诃德...,并停留在设置同样的,如果我点击大仲马之后。反之亦然:如果我将鼠标悬停或点击大仲马,然后的数据文本的设置为......迪马......如果我点击堂吉诃德不会改变。 (其它类型的时刻是空的。)

Right now it works like this: if I hover on or click "Don Quixote", then data-text is set to "... Don Quixote ...", and it stays the same if I click "Dumas" later. And vice versa: if I hover on or click "Dumas", then data-text is set to "... Dumas ..." and doesn't change if I click "Don Quixote". (Other types are empty at the moment.)

所以,鸣叫按钮只改变我第一次运行该脚本,但我需要它被多次作为类型的变化进行更新。

So, the Tweet Button is only changed the first time I run the script, but I need it to be updated as many times as the type changes.

推荐答案

我这个奋斗了今天上午几个小时,但终于得到了它的工作!这个问题基本上是,你只能包括叽叽喳喳的 widgets.js 的剧本曾经的页面,该脚本评估数据文本负载属性。因此,在你的榜样,你动态设置数据文本属性之前的 加载脚本,预期这将正常工作。但是,您可以再作任何进一步的更新脚本已经运行。

I struggled with this for a couple of hours this morning, but finally got it working! The problem is essentially that you can only include the twitter widgets.js script once in the page, and that script evaluates the data-text attribute on load. Therefore, in your example, you dynamically set the data-text attribute before loading the script, which will work as expected. However, you can then make no further updates as the script has already run.

我看到 本文 建议你可以叫 twttr。 widgets.load()再次在运行时重新评估和重新渲染的按钮,但是这并没有为我工作。这是因为该功能重新评估&LT; A&GT; 标签,而不是&LT; IFRAME&GT; <!/ code>标记

I saw this article suggesting you can call twttr.widgets.load() again at runtime to re-evaluate and re-render the buttons, however that didn't work for me. This is because that function re-evaluates <a> tags, not <iframe> tags!

因此​​,解决方案,因为 在此间指出, ,就是要彻底清除呈现&LT; IFRAME&GT; 从DOM,然后作出一个新的&LT; A&gt;在所有适当的属性元素之前调用 twttr.widgets.load()终于重新评估它,把它变成一个&LT; IFRAME方式&gt;

So the solution, as pointed out here, is to completely remove the rendered <iframe> from the DOM, then make a new <a> element with all the appropriate attributes before calling twttr.widgets.load() to finally re-evaluate it and turn it into an <iframe>.

请参见 此琴 一个工作的例子!

Please see this fiddle for a working example!

这篇关于动态更改资料Tweet按钮&QUOT;数据文本&QUOT;内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆