Javascript 推特共享URL膨胀在按钮单击功能中不起作用
我目前正在开发一个报价应用程序Jquery getJson,用于对元素进行充气。我无法将twitter的共享按钮集成到我的代码中。由于某些原因,当我使用attr函数更改tweet URL数据文本值时,我无法让它从API源获取文本值。当我单击tweet按钮时,我得到的只是一个带有硬编码文本的新tweet窗口。我的目的是在文本字段中设置quote+作者。有什么办法让它工作吗?我对tweet事件做了很多研究和实验,但没有成功 我的HTML代码:Javascript 推特共享URL膨胀在按钮单击功能中不起作用,javascript,jquery,asynchronous,twitter,Javascript,Jquery,Asynchronous,Twitter,我目前正在开发一个报价应用程序Jquery getJson,用于对元素进行充气。我无法将twitter的共享按钮集成到我的代码中。由于某些原因,当我使用attr函数更改tweet URL数据文本值时,我无法让它从API源获取文本值。当我单击tweet按钮时,我得到的只是一个带有硬编码文本的新tweet窗口。我的目的是在文本字段中设置quote+作者。有什么办法让它工作吗?我对tweet事件做了很多研究和实验,但没有成功 我的HTML代码: <html class="no-js" lang=
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<style>
body {
padding-top: 50px;
padding-bottom: 20px;
}
</style>
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" href="css/main.css">
<!--[if lt IE 9]>
<script src="js/vendor/html5-3.6-respond-1.4.2.min.js"></script>
<![endif]-->
</head>
<body>
<div id="btn-container" class="container">
<p><a id="btn1" class="btn btn-primary btn-lg" href="#" role="button">Quote Me</a></p>
</div>
<div id="quote-container" class="container">
<blockquote id="quote">Never do anything yourself that you can hire someone else to do, especially if they can do it better.
</blockquote>
<footer id="author">Bill Bernback</footer>
</div>
<div id="share-container" class="container">
<a href="https://twitter.com/share" class="twitter-share-button" data-text="Something other than page title">Tweet it</a>
</div>
<script async src="http://platform.twitter.com/widgets.js" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.11.2.min.js"><\/script>')</script>
<script src="js/vendor/bootstrap.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
<pre><code>var quote;
var author
$(document).ready(function(){
$(".btn").click(function(){
$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_jsonp=?", function(getQuote) {
quote = getQuote[0].content;
author = getQuote[0].title;
$("#quote").html(quote);
$("#author").text(author);
$('.twitter-share-button').attr('data-text',author); // not working
});});});
这是什么意思
quote=getQuote[0].content代码>执行控制台时返回。log
@DavidR日志正常,我得到了正确的字符串。此日志返回p元素中的随机引号。问题是:我不能让attr在这个函数中工作来设置我的tweet文本:(你能试试$('.twitter share button').data('text',author)
?Sry,不起作用。我不认为问题出在data inflator函数上,我已经尝试了其他类似的方法这是什么.content;return当您执行console.log
@DavidR日志正常时,我得到了正确的字符串。此日志在p元素中返回一个随机引号。问题是:我无法使attr在此函数中工作以设置我的推文文本:(您可以尝试$('.twitter共享按钮')。data('text',author)
?Sry不起作用。我不认为问题出在数据充气功能上,我已经尝试过其他类似的功能