Javascript 当Chrome开发者工具打开时,有些东西会断裂

Javascript 当Chrome开发者工具打开时,有些东西会断裂,javascript,jquery,html,css,google-chrome,Javascript,Jquery,Html,Css,Google Chrome,我制作了一个报价生成器,它可以在Chrome开发者工具打开时正常工作,但在开发者工具关闭时不会生成新报价。这发生在我的CodePen项目中。在我的电脑上,它会生成一个报价三次(在前三次点击“生成报价”按钮时工作正常),然后停止工作。在Safari中根本不起作用。为什么会这样 我相信我的JavaScript也可以使用一些重构,任何帮助都会很好。谢谢 HTML JavaScript $(document).ready(function(){ //get quote from random

我制作了一个报价生成器,它可以在Chrome开发者工具打开时正常工作,但在开发者工具关闭时不会生成新报价。这发生在我的CodePen项目中。在我的电脑上,它会生成一个报价三次(在前三次点击“生成报价”按钮时工作正常),然后停止工作。在Safari中根本不起作用。为什么会这样

我相信我的JavaScript也可以使用一些重构,任何帮助都会很好。谢谢

HTML

JavaScript

$(document).ready(function(){
     //get quote from random quote API
      $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
      //append quote and author to document
      $(".quote").append(a[0].content + "<p>&mdash; " + a[0].title + "</p>")

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


      //insert tweet button
      insertTweetBtn();
    });
}); 

$("a").click(function(){
      //get quote from random quote API
      $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
      //replace HTML with newly generated quote
      $(".quote").html(a[0].content + "<p>&mdash; " + a[0].title + "</p>")
      //remove contents of tweet button div
      $("#twtbtn").empty();
      //insert new tweet button to grab newly generated quote
      insertTweetBtn();
    });
 }); 


function insertTweetBtn() {
    var msg = document.getElementById('msg').textContent;
    twttr.ready(function (twttr) {
            twttr.widgets.createShareButton(
                '',
                document.getElementById('twtbtn'),
                function (el) {
                    console.log("Button created.")
                },
                {
                    text: msg ,  
                }
            );
            twttr.events.bind('tweet', function (event) {
                console.log(event, event.target);
            });
        });

}
$(文档).ready(函数(){
//从随机报价API获取报价
$.getJSON(“http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=“,函数(a){
//将引用和作者附加到文档
$(“.quote”).append(a[0].content+“&mdash;”+a[0].title+“

”) //启动twitter按钮功能 window.twttr=(函数(d,s,id){ var t,js,fjs=d.getElementsByTagName[0]; if(d.getElementById(id))返回;js=d.createElement;js.id=id; js.src=”https://platform.twitter.com/widgets.js“fjs.parentNode.insertBefore(js,fjs); return window.twttr | |(t={ue:[],ready:function(f){t.e.push(f)}); }(文件,“脚本”、“推特wjs”); //插入tweet按钮 insertTweetBtn(); }); }); $(“a”)。单击(函数(){ //从随机报价API获取报价 $.getJSON(“http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=“,函数(a){ //用新生成的报价替换HTML $(“.quote”).html(a[0].content+“&mdash;”+a[0].title+“

”) //删除tweet按钮div的内容 $(“#twtbtn”).empty(); //插入新tweet按钮以获取新生成的报价 insertTweetBtn(); }); }); 函数insertTweetBtn(){ var msg=document.getElementById('msg').textContent; twttr.就绪(功能(twttr){ twttr.widgets.createShareButton( '', document.getElementById('twtbtn'), 功能(el){ log(“创建了按钮”) }, { 文本:msg, } ); twttr.events.bind('tweet',函数(事件){ 日志(事件、事件、目标); }); }); }
好吧……所以我想弄明白这一点,真是疯了,然后我去看了getJSON for jquery的文档。我对JSONP不是百分之百感兴趣,但当您将&callback添加到url中时,就是这样。它使用JSONP。所以我移除了它,它在safari中非常有效。这是密码笔

以下是jquery的报价:

如果URL包含字符串“callback=?”(或类似字符串,由服务器端API定义),则请求将改为JSONP。有关详细信息,请参阅$.ajax()中对JSONP数据类型的讨论

不幸的是,这并不能解释为什么safari是唯一一个它不工作的工具……但是,嘿,它修复了它:)

body {
    background: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('https://images.unsplash.com/photo-1437652010333-fbf2cd02a4f8?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&s=2330269f135faf1c33bf613b85d5f1df');
    background-size:     cover;              
    background-repeat:   no-repeat;
    background-position: center center; 
}

* {
    font-family: 'Lato', sans-serif;
}

.quote-container {
    display: flex;
    flex-direction: column;
    justify-content: center;    
}

.quote {
    width: 80%;
    text-align: center;
    margin: 0 auto;
    font-size: 48px;
    font-style: italic;
    color: white;
}

.button-container {
    margin: 30px auto 50px auto;
    text-align: center;
}

#button {
    border: 1px solid white;
    padding: 12px 30px;
    background: transparent;
    font-size: 18px;
    border-radius: 2px;
}

#button:hover {
    background-color: white;
}

a {
    text-decoration: none;
    color: white;
}

a:hover {
    color: black;
};
$(document).ready(function(){
     //get quote from random quote API
      $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
      //append quote and author to document
      $(".quote").append(a[0].content + "<p>&mdash; " + a[0].title + "</p>")

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


      //insert tweet button
      insertTweetBtn();
    });
}); 

$("a").click(function(){
      //get quote from random quote API
      $.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
      //replace HTML with newly generated quote
      $(".quote").html(a[0].content + "<p>&mdash; " + a[0].title + "</p>")
      //remove contents of tweet button div
      $("#twtbtn").empty();
      //insert new tweet button to grab newly generated quote
      insertTweetBtn();
    });
 }); 


function insertTweetBtn() {
    var msg = document.getElementById('msg').textContent;
    twttr.ready(function (twttr) {
            twttr.widgets.createShareButton(
                '',
                document.getElementById('twtbtn'),
                function (el) {
                    console.log("Button created.")
                },
                {
                    text: msg ,  
                }
            );
            twttr.events.bind('tweet', function (event) {
                console.log(event, event.target);
            });
        });

}
$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1", function(a) {stuff}