javascript - 调用 API 无需重新加载页面

标签 javascript jquery html api

HTML

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Rokkitt" rel="stylesheet"> 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="styles1.css" />
    <script src="api.js"></script>
</head>

<body background="texture.jpg" class="bg">
    <div class="container">
        <h1> Awesome Quotes</h1>
        <p>Your daily dose of wisdom.</p>
        <div class="dynamic">
            <button class="btn btn-primary" id="button" type="submit">Get a New Quote</button>
        </div>
        <div class="quote"></div>
        <a href="https://twitter.com/intent/tweet?hashtags=quotes" target="_blank">
        <i class="fa fa-twitter"></i></a>
    </div>
</body>

</html>

JS

$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
  $(".quote").append(a[0].content + "<p>— " + a[0].title + "</p>")
});

这些是我的 HTML 和 JS。我想做的是在 div 内生成随机引用。我为此调用了一个 API。每次重新加载页面时,都会出现一个随机引用。我需要做什么是单击“获取新报价”按钮并获取报价,而无需使用 jquery 重新加载页面。另外,我需要单击 twitter 图标将我带到 twitter,在那里我可以在编辑器中看到相关报价,以便发布。

最佳答案

HTML

为您的按钮指定一个类,以便您可以轻松访问它

<button class="btn btn-primary getnewquote" id="button" type="submit">Get a New Quote</button>

脚本

var Res;
    $('.getnewquote').click(function(){
    //your ajax call
    $.ajax({
        type: "POST",
        url: "Your web service url",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (json) {
            Res = JSON.parse(json.d);
            $(".quote").append(Res[0].content + "<p>— " + Res[0].title + "</p>")


        },
        failure: function (msg) {
            console.log(msg);
        }
    });
    });

关于javascript - 调用 API 无需重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49133239/

相关文章:

javascript - 如何发送 Raphael 元素 .toBack()

javascript 动态属性在从 json 成功声明后消失 - 变成空对象(不是未定义的)

javascript - 轮播导航触发 Bootstrap 的 Affix 函数

javascript - Uncaught ReferenceError : google is not defined

javascript - .html 和 .text 不起作用

javascript - Twitter 小部件完全加载后调用函数

html - 在存在边距的情况下设置子项相对于父项的宽度

javascript - 按钮不从中心动画

javascript - 为什么 JQuery 自动完成结果不显示在浏览器中?

javascript - 如何使用 Chart.js 向我的折线图添加点击事件