jQuery点击仅发生一次

时间:2016-09-27 15:31:52

标签: javascript jquery html css

我正在创建一个随机引用生成器。每次单击“获取引用”按钮时,都应该从API生成新引号,并更改背景颜色。但是,虽然每次单击时背景颜色都会更改,但只有在我第一次单击按钮时才会更改引号。

我的HTML如下:

<body>
<div class="container-fluid">
  <div id="centerThis" style="background: transparent">
    <div id=target1>
      <h1 class="text-center">Random Quote:</h1>
      <div class="quote">
        <blockquote>
          <p id="quoteT">Humans are allergic to change. They love to say, 'We've always done it this way.' I try to fight that. That's why I have a clock on my wall that runs counter-clockwise.</p>
          <footer id="quoteA">Grace Hopper</footer>
      </blockquote>
      </div>
      <div class="row">
        <div class="col-xs-2">
          <a href="https://twitter.com/?lang=en" target='_blank'><button onclick="tweetIt()" class="btn myButton colorButton"><i class="fa fa-twitter" aria-hidden="true"></i></button></a>
        </div>
        <div class="col-xs-2">
          <a href="https://www.tumblr.com/dashboard" target='_blank'><button onclick="tweetIt()" class="btn myButton colorButton"><i class="fa fa-tumblr" aria-hidden="true"></i></button></a>
        </div>
        <div class="col-xs-4">
        </div>
        <div class="col-xs-4">
          <button id = "getQuote" class = "btn colorButton">
        Get Quote
      </button>
        </div>
      </div>
    </div>
    <div id="target2">
      <h5 id="byAnna" class="text-center">by <a href="http://codepen.io/annajolly/" target="_blank">anna</a></h5>
    </div>
  </div>
</div>
</body>

我的jQuery看起来像这样(我省略了randColor和其他一些非重要部分的代码):

$(document).ready(function() {
  $("#target1").css("color",randColor);
  $("#target2").css("background", randColor);
  $("body").css("background", randColor);
  $(".colorButton").css("background", randColor);
  $("#getQuote").on("click", function(){
    randColor = getRandomColor();
    $("#target1").css("color",randColor);
    $("#target2").css("background", randColor);
    $("body").css("background", randColor);
    $(".colorButton").css("background", randColor);
    $.ajax({url: "http://crossorigin.me/http://api.forismatic.com/api/1.0/?method=getQuote&key=457653&format=json&lang=en", success: function(result){
            $(".quote").html("<blockquote><p>" + result.quoteText + "</p><footer>" + result.quoteAuthor + "</footer></blockquote>");
        }});
    });
});

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您的网址似乎有误:

http://crossorigin.me/ http://api.forismatic ...

每次单击按钮时,您是否在Web检查器上看到了Ajax请求?

此致

编辑:

问题是文字颜色: 它与bg相同。

尝试在JS中添加这一行:

$(&#34; .quote&#34;)。css(&#34; color&#34;,&#34; black&#34;);