第三个引号中有一个半殖民地,引号显示正确,但没有正确推文。它在分号处截断。
$(document).ready(function(){
var randQuote;
var randAuthor;
var randNum;
function getQuote(){
var quote =["While you are destroying your mind watching the worthless, brain-rotting drivel on TV, we on the Internet are exchanging, freely and openly, the most uninhibited, intimate and, yes, shocking details about our config.sys settings", "The secret of getting ahead is getting started.", "The unforgivable crime is soft hitting. Do not hit at all if it can be avoided; but never hit softly.", "The man on the top of the mountain didn't fall there.", "You can avoid most of the sorrows of life, the only requirement being that you avoid all the happiness."];
var author =["-Dave Barry", "-Mark Twain", "-Theodore Roosevelt","-Vince Lombardi", "-Robert Brault"];
var randNum = Math.floor(Math.random()*quote.length);
randQuote = quote[randNum];
randAuthor = author[randNum];
$(".quote").text(randQuote);
$(".author").text(randAuthor);
}
$("#newQuote").on("click", function(){
getQuote();
});
$("#tweet").on("click", function(){
var tweetQuote = randQuote;
var currentLength = randQuote.length + randAuthor.length + 1;
if(currentLength > 140){
var oneforty = 139 - randAuthor.length ; // 140 minus the space
tweetQuote = tweetQuote.slice(0, oneforty);
}
window.open("https://twitter.com/intent/tweet?text=" + tweetQuote + " " + randAuthor);
})
});
答案 0 :(得分:0)
您需要在查询通过网址时转义查询。
您可以对encodeURIComponent()
或escape()
使用可能包含特殊字符的任何变量。
您的代码将如下所示
window.open("https://twitter.com/intent/tweet?text=" + encodeURIComponent(tweetQuote) + " " + encodeURIComponent(randAuthor));
选中此示例:
https://jsfiddle.net/nabtron/duyzk98b/2/
<强>更新强>
请勿使用escape()
,因为自ECMAScript v3以来已弃用。