0
我想让推文按钮分享我的报价机器的报价。为此,我试图改变它的href
有; -
$("#twitter-widget-0").attr("href", "https://twitter.com/intent/tweet?ext=" + thisQuote);
,但由于某种原因,这是行不通的。可能是什么问题。
P.S. "#twitter-widget-0"
未在我的html中定义,但是这是Twitter按钮创建的呈现的iframe的ID。
这里是我的笔:http://codepen.io/s4ek1389/pen/zZGNWw?editors=0110
HTML:
<a href="https://twitter.com/share" class="twitter-share-button" data-size="large">Tweet</a>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
JS:
$(document).ready(function(){
$("#gen").on("click", function(){
$.ajaxSetup({cache:false});
$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(json) {
var author = "";
var quote = "";
json.forEach(function(val){
author+= val.title;
quote+= val.content;
})
$("#author").html(author);
$("#mainquote").html(quote);
var thisQuote = quote + " " + author;
$("#twitter-widget-0").attr("href", "https://twitter.com/intent/tweet?ext=" + thisQuote);
});
});
});
关于你的postscript'#twitter-widget-0'没有在你的html中定义,那个元素创建的是什么时候?你不能使用'$(“#twitter-widget-0”)。attr()'来更新一个还不存在的元素。 – nnnnnn
感谢您指出这一点,但将“#twitter-widget-0”更改为“.twitter-share-button”或甚至“a”仍然没有帮助 –
是的,我需要更改'a'的href标签。我的链接是“twitter.com/share”我需要将其更改为“https://twitter.com/intent/tweet?ext=”+ thisQuote; –