【发布时间】:2021-05-01 05:40:20
【问题描述】:
这是我下面的代码。在单击按钮之前,我的 getQuote 一直在触发。它在页面加载时立即触发。这对我来说很奇怪,有什么想法吗?
function _(id) {
return document.getElementById(id);
}
var html = "";
const newQuote = document.getElementById("submit");
const tweet = _("tweet-quote");
var tweetURL = "https://twitter.com/intent/tweet?hashtags=RonSwanson&text=";
newQuote.onclick = getQuote();
//Get Quote Function
function getQuote() {
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'https://ron-swanson-quotes.herokuapp.com/v2/quotes')
ourRequest.onload = () => {
var ourData = JSON.parse(ourRequest.responseText);
quoteText = ourData[0];
html = "<i class='fas fa-quote-left'></i>"
html += ourData[0] + "<i class='fas fa-quote-right'></i>";
html += "<br><br>" + "<p class='text-right'> -Ron Swanson</p>";
_("quote").innerHTML = html;
tweet.setAttribute('href', tweetURL + encodeURIComponent('"' + quoteText + '"') + encodeURIComponent("-Ron Swanson"));
}
ourRequest.send();
}
【问题讨论】:
标签: javascript function onclick