简体   繁体   中英

Javascript Tweet Button not linking

i wonder whats wrong with my tweet button can somebody check my code? the link does not generate the tweet.

check my code here.

http://codepen.io/mochiii/pen/YyqmJP

$(document).ready(function(){
  $(".tweet-hold a").hide();
  $("button").click(function(){
    $.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?", function(response){

      $(".quote").html("<p>" + response.quoteText + "</p>");
      $(".author").html("<p>" + response.quoteAuthor + "</p>");
    });
    $(".tweet-hold a").show();
    var tweet="http://twitter.com/intent/tweet?text=" + response.quoteText;
    if(response.quoteAuthor!==""){
      tweet+=" - " + response.quoteAuthor;
    }
    $(".tweet-hold a").click(function(tweet){
      $(this).attr('href', tweet);

    });
  });
});

it seems you need to modify your code a bit.

$(document).ready(function(){
  $(".tweet-hold a").hide();
  $("button").click(function(){
    $.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?", function(response){

      $(".quote").html("<p>" + response.quoteText + "</p>");
      $(".author").html("<p>" + response.quoteAuthor + "</p>");
    });
    $(".tweet-hold a").show();
    var tweet="http://twitter.com/intent/tweet?text=" + response.quoteText;
    if(response.quoteAuthor!==""){
      tweet+=" - " + response.quoteAuthor;
    }
  });

  $(".tweet-hold a").click(function(tweet){
      $(this).attr('href', tweet);

   });
});

That seemed to work fine, when I tried in your codepen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM