.click()仅触发一次,然后再触发一次,除非我刷新整个页面并且如果我调用$(document).ready()则完全不渲染。

时间:2020-03-14 12:09:15

标签: javascript jquery

我正在尝试从JSON文件中检索引号及其作者,我遇到的问题是,一旦单击按钮一次,就会触发该事件,但之后仅触发一次。同样,当我调用$(document).ready()时,该按钮完全不会触发。

我知道这与我从JSON文件中收集数据(是一个带有文本,作者键的对象数组),然后在我想检索新报价时调用fetch的方式有关与正确的作者。

// https://type.fit/api/quotes //
// api link for quotes //
const button = document.getElementById('new-quote');
const baseUrl = 'https://type.fit/api/quotes';
let randomNumber = Math.floor(Math.random() * 100);

function getQuote() {
 fetch(baseUrl)
  fetch(baseUrl)
  .then(response => response.json())
  .then(quote => $('#text-output').text(quote[randomNumber].text))
   };

function getAuthor() {
  fetch(baseUrl)
  .then(response => response.json())
  .then(quote => $('#author').text(quote[randomNumber].author))
};

function renderQuoteToPage() {
  getQuote();
  getAuthor();
}


$('#new-quote').click(renderQuoteToPage);
$(document).ready(renderQuoteToPage);
body {
  width: 100%;
  height: auto;
}

#outer-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="outer-wrapper" class="container-fluid">

<div id="quote-box" class="card w-50">
  <div class="card-body">

  <div class="row">
    <div class="col">
  <div id="text">
    <p id="text-output"></p>
      </div>
    </div>
  </div>
  
    <div class="row">
      <div class="col">
      <a href="#" id="tweet-quote">Tweet</a>
      </div>
      <div class="col">
      <div id="author">Author</div>
      </div>
  </div>
  
  <div class="row">
    <div class="col">
  <button id="new-quote">New quote</button>
    </div>
  </div>
  
  </div>
</div>
  
</div>

2 个答案:

答案 0 :(得分:3)

在附加处理程序时,您正在调用函数,因此在没有附加适当的处理程序的情况下仅调用一次它们。

您只需要函数定义:

$('#new-quote').click(renderQuoteToPage);
...
$(document).ready(renderQuoteOnPageLoad);

编辑:您还需要在每个randomNumber通话中设置getQuote()

// https://type.fit/api/quotes //
// api link for quotes //
const button = document.getElementById('new-quote');
const baseUrl = 'https://type.fit/api/quotes';
let randomNumber

function getQuote() {
  randomNumber = Math.floor(Math.random() * 100);
  fetch(baseUrl)
    .then(response => response.json())
    .then(quote => $('#text-output').text(quote[randomNumber].text))
};

function getAuthor() {
  fetch(baseUrl)
    .then(response => response.json())
    .then(quote => $('#author').text(quote[randomNumber].author))
};

function renderQuoteToPage() {
  getQuote();
  getAuthor();
}


$('#new-quote').click(renderQuoteToPage);
$(document).ready(renderQuoteToPage);
body {
  width: 100%;
  height: auto;
}

#outer-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="outer-wrapper" class="container-fluid">

  <div id="quote-box" class="card w-50">
    <div class="card-body">

      <div class="row">
        <div class="col">
          <div id="text">
            <p id="text-output"></p>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col">
          <a href="#" id="tweet-quote">Tweet</a>
        </div>
        <div class="col">
          <div id="author">Author</div>
        </div>
      </div>

      <div class="row">
        <div class="col">
          <button id="new-quote">New quote</button>
        </div>
      </div>

    </div>
  </div>

</div>

答案 1 :(得分:1)

脚本中的少量更改及其完成。

java -cp target/gs-serving-web-content-0.1.0.jar com.pkg.subpkg.MainClass

Example Fiddle