我正在做一个随机报价生成器。但我无法通过Twitter分享它。我收到一个推特页面,但推文的文本框是空的。随机报价生成器:按钮与Jquery
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>title</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="div1">
<p id="quote"></p>
<p id="author"></p>
<button id="btn">getquote</button>
<div>
<a href="https://twitter.com/intent/tweet" target="_blank">
<button type="button" id="twitter-share-button">tweet</button>
</a>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="script.js"></script>
</body>
</html>
这里是我的Javascript代码
var url1="http://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=jsonp&lang=en&jsonp=?";
//var url1=" http://date.jsontest.com/";
var getQuote=function(data){
//console.log(data);
var quot="https://twitter.com/intent/tweet?text=" + data.quoteText+data.quoteAuthor;
console.log(quot);
$('#quote').text(data.quoteText);
$('#author').text(data.quoteAuthor);
$('#twitter-share-button').attr("href", quot);
};
$(document).ready(function(){
$.getJSON(url1,getQuote,'jsonp');
});
$("#btn").on("click",function(){
$.getJSON(url1,getQuote,'jsonp');
});
我得到的报价随意,但在我的代码点击鸣叫按钮犯规jQuery中使用.attr
改变href
。我做得对吗?
你怎么知道是不是被添加到'href'?我刚刚制作了一个非常简单的JSFiddle来显示你的方法应该工作得很好:https://jsfiddle.net/errd1p37/1/。这也给我带来了我的下一个问题:你为什么要在'button'中添加'href'? – Santi
增加'href'。我可以在控制台中看到。但是,当我点击按钮时,它会转到“https:// twitter.com/intent/tweet”,而不是“https://twitter.com/intent/tweet?text=”+ data.quoteText + data.quoteAuthor'。所以我没有在文本框中看到鸣叫 – vish09
您正在将'href'应用于'