javascript - Getting jquery to add the content of two elements automatically to a button link
问题描述
I´m running this code, and I keep getting "undefined" when I click on my tweet button to redirect me to twitter. Any ideas on how to fix it and getting the desired outcome?
function startQuote() {
$.getJSON(
"https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&_jsonp=?",
function (data) {
// The data is an array of posts. Grab the first one.
var post = data.shift();
$("#author").text(post.title);
$("#text").html(post.content);
$("#tweet-quote").attr(
"href",
'https://twitter.com/intent/tweet?text="' +
post.title.text +
'"' +
" - " +
post.content.text
);
}
);
}
window.onload = startQuote;
$("#new-quote").on("click", startQuote)
解决方案
post.title.text and post.content.text are undefined
$("#tweet-quote").attr("href", "https://twitter.com/intent/tweet?text=\"" + post.title + "\"" + " - " + $(post.content).text());
推荐阅读
- python - 初学者石头剪刀布游戏 Python 语法
- c# - 如何获取从 MessagingFactory Azure 创建的接收器数量的详细信息
- javascript - 需要将小于大于标记转换为日文行中的实体
- swift - Swift 中不同 iOS 设备的不同 UIFont 大小
- java - Opendaylight 氧气安装资源 API 未定义
- r - SuperLearner 只提供一个离散的赢家
- apache-kafka - Kafka (0.10.0.1) Mirror Maker 不工作,似乎进程正在运行但什么也没做
- django - KeyError:如何在 form_invalid() 中正确设置变量
- css - Webdriver.io 如何根据页面上类似元素的数量选择和更改元素
- regex - 正则表达式:捕获域 example.com 的任何 URL