这个问题与我想要的非常接近:Replace an Attribute in the Tweet Button with Jquery
然而,the suggested solution只能工作一次。也就是说,我不能在switch语句中使用它,如下所示:
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');
});
...
}
发生的情况是,data-text属性是根据最先发生的情况设置的,之后不会更改。
如何根据需要多次更改Tweet Button的数据文本属性?
更新:这是我正在处理的页面:http://zhilkin.com/socio/en/
可以安全地忽略特征表。我想对Sociotype表做的是,当您单击一个类型时,右侧描述下面的Tweet按钮的data-text应该相应地更改。
现在它的工作原理是这样的:如果我悬停或点击"Don Quixote",那么data-text被设置为"... Don Quixote ...",如果我稍后点击"Dumas“,它将保持不变。反之亦然:如果我悬停在“Dumas”上或单击"Dumas",则data-text设置为"... Dumas ...“如果我点击“唐吉柯德”也不会改变。(其他类型目前为空。)
因此,Tweet Button只有在我第一次运行脚本时才会更改,但我需要随着类型的更改而多次更新它。
发布于 2012-05-11 20:08:03
今天早上我为此挣扎了几个小时,但最终还是让它工作了!本质上的问题是,您只能在页面中包含一次的推特widgets.js脚本,并且该脚本会在加载时计算data-text
属性。因此,在您的示例中,在加载脚本之前动态设置data-text
属性,这将按预期工作。但是,由于脚本已经运行,因此不能进行进一步的更新。
我看到建议您可以在运行时再次调用twttr.widgets.load()
来重新评估和重新呈现按钮,但是这对我不起作用。这是因为该函数重新计算的是<a>
标签,而不是<iframe>
标签!
因此,作为,解决方案是从DOM中完全删除呈现的<iframe>
,然后在调用twttr.widgets.load()
之前创建一个具有所有适当属性的新<a>
元素,以最终重新计算该元素并将其转换为<iframe>
。
有关工作示例,请参阅!
发布于 2017-01-03 17:39:59
你也可以使用推特的createShareButton接口调用:
function callAsRequired(){
var nodeID = 'YourTwitterNodeID'
//Remove existing share button, if it exists.
var myNode = document.getElementById(nodeID);
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
//Create button and customise
twttr.widgets.createShareButton(
'http://your.custom.url.here/',
document.getElementById(nodeID),
{
count: 'none',
text: 'Your custom tweet here'
});
}
发布于 2012-05-08 01:36:50
由于您使用的是each
循环,因此可以使用if
语句:
$(document).ready(function(){
$('a[data-text]').each(function(){
if (theCase == 1) {
$(this).attr('data-text', Text_Variant_1);
}
else if (theCase == 2) { ... }
})
});
https://stackoverflow.com/questions/10486354
复制相似问题