无法编辑Tweet按钮的href参数

时间:2016-03-12 19:04:57

标签: jquery html twitter

我正在编写随机引用应用程序,必须与作者一起显示随机引用,然后编辑Tweet按钮的 href 参数,但我无法设法获取它,我尝试使用jQuery的.attr()函数,#b(按钮的id),但仍然没有任何反应。这是一个简单的例子:



$(document).ready(function() {
  var quotes = {"Steve Jobs": "Things don't have to change the world to be important.",
          "Sigmund Freud":" If youth knew; if age could.",
          "Albert Einstein": "You can't blame gravity for falling in love.",
          "Amelia Earhart": "Women, like men, should try to do the impossible. And when they fail, their failure should be challenge to others.",
          "Mahatma Gandhi": "Where there is a love, there is a life.",
          "Buddha": "Do not dwell in the past, do not dream of the future, concentra the mind on present moment.",
          "Buddha": "The mind is everything. What you think you become.",
          "Winston Churchill": "Success consists of going from failure to failure without loss of enthusiasm.",
          "Audrey Hepburn": "Nothing is impossible, the word itself says 'I'm possible'!",
          "Milton Berle": "If opportunity doesn't knock, build a door.",
          "Swami Sivananda": "Put your heart, mind, and soul into even your smallest acts. This is the secret of success.",
          "John F. Kenedy": "Mankind must put an end to war before war puts an end to mankind.",
          "Hugh Hefner": "Surrounding myself with beautiful women keeps me young.",
          "Mahatma Gandhi": "It is health that is real wealth and not pieces of gold and silver."
          }

  function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  var author = [];
  var quote = [];
  var tweetText = "";

  for (var a in quotes) {
    author.push(a);
    quote.push(quotes[a]);
  }

  var randomNumber = getRandomInt(0, 13);
  $("#quote-content").text(quote[randomNumber]);
  $("#quote-author").text(author[randomNumber]);

  $("#generator").click(function(){
      randomNumber = getRandomInt(0, 13);
      $("#quote-content").fadeOut("slow", function() {
        $("#quote-content").text(quote[randomNumber]);
      });
    
      $("#quote-author").fadeOut("slow", function() {
        $("#quote-author").text(author[randomNumber]);
      });
    
      $("#quote-content").fadeIn("slow");
      $("#quote-author").fadeIn("slow");
  });
});

window.twttr = (function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
  if (d.getElementById(id)) return t;
  js = d.createElement(s);
  js.id = id;
  js.src = "https://platform.twitter.com/widgets.js";
  fjs.parentNode.insertBefore(js, fjs);
 
  t._e = [];
  t.ready = function(f) {
    t._e.push(f);
  };
 
  return t;
}(document, "script", "twitter-wjs"));

body {
  background-color: #27ae60;
  color: #000000; }

cite, footer {
  color: #ecf0f1; }

#quote {
  margin-top: 10px;
  font-family: 'Roboto';
  font-size: 15pt;
  background-color: #2ecc71;
  color: #ecf0f1;
  padding: 10px;
  width: 100%;
  border: 0px; }

blockquote {
  border-left: none; }

blockquote:before {
  content: "\f10d";
  font-family: "FontAwesome";
  font-size: 20pt;
  float: left;
  margin-right: 10px; }

#generator {
  border: 0px;
  border-radius: 2px;
  background-color: #27ae60;
  color: #bdc3c7;
  transition-duration: 0.5s;
  margin-left: 5px; }

#generator:hover {
  color: #ffffff;
  background-color: #2ecc71; }

.twitter {
  float: right; }

/*# sourceMappingURL=style.css.map */

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='container'>
  <div class='panel panel-default' id='quote'>
    <blockquote>
      <div id='quote-content'>
        This is random quote
      </div>
      <footer>
        <cite id='quote-author' title='author'>
          Source
        </cite>
      </footer>
    </blockquote>
    <a class='btn btn-default' id='generator'>
      Get another quote
    </a>
    <div class='twitter'>
      <a class='twitter-share-button btn btn-default' data-size='large' href='' id='tweet-button'></a>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

0 个答案:

没有答案