我在我的網站(http://conn3cted.uk.tn/quote.html)上使用Twitter createShareButton,但每次按下「新報價」按鈕時,都會創建一個新的Twitter按鈕,並留下多個按鈕(例如,如果「新報價」按鈕被按下10次,將會有10個分享按鈕)。Twitter按鈕正在創建多個按鈕
幾天前它工作正常,所以不知道到底發生了什麼。
我的JavaScript低於
// Random Quote Generator
$(document).ready(function() {
// Set the default values for future AJAX requests
$.ajaxSetup({
// Prevent all future AJAX requests from being cached.
cache: false
});
// API URL
var quoteURL = "http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1";
// getQuote function, which accepts a data parameter.
var getQuote = function(data) {
// Replace the html of the targeted element with the returned content field.
$(".quoteContent").html(data[0].content);
// Replace the html of the targeted element with the returned title field.
$(".quoteTitle").html('Author: ' + data[0].title);
newColor();
twttr.ready(function() {
// Use the pre-made createShareButton function
twttr.widgets.createShareButton(
// Do not share a URL
" ",
// Div where the share button should be inserted
document.getElementById('tweetButton'),
{
// Define the tweet that will be used
text: "'" + $(".quoteContent").text().replace(/(\r\n|\n|\r)/gm,"") + "'\n" +
$(".quoteTitle").text(),
// Set the size of the Twitter share button
size: "large",
}
);
});
};
var newColor = function() {
var color = Please.make_color();
$("body, button, .btn").css("background-color",color);
$("p, blockquote").css("color",color);
};
/*
* On every page load get JSON data from the URL, defined in the
* 'url' variable, and then run the getQuote function
*/
$.getJSON(quoteURL, getQuote);
//newColor();
/*
* When the button is clicked load get JSON data from the * URL,defined in the 'url' variable, and then run the
* getQuote function.
*/
$(".regenButton").click(function() {
$.getJSON(quoteURL, getQuote);
});
});
啊好吧!所以它不明白,它應該只創建一個 - 我認爲我混淆了HTML小部件設置(它只做一次)與這個按鈕不應該創建多個的事實。 但這是有道理的 - 它正在創建一個新的按鈕,因爲它已被告知要做,只是將其附加到指定的'div' 我添加了額外的行,現在所有的工作:-)謝謝!你可以說,我還在學習。有時我犯了愚蠢的錯誤。 – OmisNomis
不客氣,看來你現在明白了這個問題!祝你好運。 – Pimmol