2016-04-05 13 views
2

Sitemdeki (http://conn3cted.uk.tn/quote.html) Twitter'da createShareButton kullanıyorum ama her zaman 'Yeni Alıntı' düğmesine basıldığında, birden çok düğmeyle beni bırakarak yeni bir twitter düğmesi oluşturuluyor (örn. 'Yeni Alıntı' düğmesine 10 kez basıldığında, 10 adet paylaşım düğmesi olacak).Twitter düğmesi birden çok düğme oluşturuyor

Birkaç gün önce gayet iyi çalışıyordu, bu yüzden ne olduğunu anlayamadık. böylece her zaman .regenButtongetQuote denir, tıklandığında, sana başka bir düğmeye vererek -

// 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); 
    }); 


}); 

cevap

1

Bu, $.getJSON başarı geriçağırımınızda twitter düğmesini oluşturduğunuzdan. Yani bir teklif yüklendiğinde, bir twitter düğmesi oluşturulur.

En iyi çözüm, düğmeyi bir kez oluşturmak ve her teklifin yüklendiği her paylaşım metnini güncellemek olacaktır. Hangi dokümanlar içinde bulamıyorum. Öyleyse benim en iyi tahminim önce bir tane önce silmek:

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(); 

    // remove previous button 
    $('.twitter-share-button').remove(); 

    // and create a new one 
    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", 
      } 
    ); 
    }); 
}; 
+0

Ah doğru tamam! Bu yüzden sadece bir tane oluşturması gerektiğini anlamıyor - bence HTML parçacığı kurulumunu (sadece bir kez yapıyor) bu düğmenin birden fazla oluşturmaması gerektiği ile karıştırıyordum. Ama bu mantıklı - yeni bir düğme yarattığı söylendi, sadece belirtilen 'div' 'a ekledim, bu ekstra satırı ekledim ve şimdi çalışıyorum :-) teşekkürler! Anlatabileceğiniz gibi, hala öğreniyorum. Bazen aptalca hatalar yaparım. – OmisNomis

+0

Bir şey değil, şimdi sorunu anlıyor gibisiniz! İyi şanslar. – Pimmol

2

Kişisel twitter paylaş düğmesi kodu getQuote işlevi içinde aşağıda

Benim JavaScript'tir.

+1

Ah ... Bir A etiket oluşturmak için

<!-- - href will create a link to the Twitter web intent composer - target (_blank) will open the link in a new window --> <a class="tweetButton" href="https://twitter.com/intent/tweet?text=Initial Text" target="_blank"> <!-- Create a button for the link to interact with --> <button class="btn"> Tweet </button> </a> 

Ve sonra kurulum jQuery her yeni teklifi için bunu değiştirmeye gerek! Bu mantıklı. Bu işlevi yerine getirmek için uygun yer neresidir? Bunu $ .getJSON (quoteURL, getQuote); altına eklemeyi denedim, ancak üretilen alıntıyı aşağı çekmiyor. – OmisNomis

+0

büyük olasılıkla, twitter düğmeniz, quote düğmesine basılana kadar herhangi bir içeriğin bulunmadığı '.quoteContent 'referansını verdiğinden dolayı, bir önceki twitter düğmesini, tekrar oluşturmadan önce kaldırabilir veya içeriğini sonradan değiştir. – Stuart

+0

Yardımlarınız için teşekkür ederiz :) @Pimskie – OmisNomis

0

Bu gönderiye giren herkes için; Bunu biraz daha araştırıyordum ve Twitter Widget'ı kullanmadan Twitter Share butonunu oluşturmanın bir yolunu keşfettim.

Önce sağ ok ...

/* 
* Amend the "href" attribute of the element with a "tweetButton" class 
* (the "a" tag) to take the twitterURL Global Variable, plus the parameter 
* "text=" (which specifies what the tweet will say) and assign the value of: 
* 
* - the element with a class of "quoteContent", without the HTML tags 
* (.text()), removing all line breaks (.replace(/(\r\n|\n|\r)/gm,"")), 
* trims all the white space (.trim()) 
* 
* - adds a line break (\n) 
* 
* - the element with a class of "quoteTitle", without the HTML tags 
* (.text()) 
* 
* Then URL encode the result (encodeURIComponent) to complete the amendment 
*/ 
$(".tweetButton").attr("href", twitterURL + "text=" + encodeURIComponent("'" + 
    $(".quoteContent").text().replace(/(\r\n|\n|\r)/gm,"").trim() + 
    "'\n" + $(".quoteTitle").text())); 
İlgili konular