2016-06-23 16 views
-1

私は見積もりジェネレーターを作成しましたが、Chromeデベロッパーツールを開いた状態で問題なく動作しますが、デベロッパーツールが閉じられても新しい見積もりは生成されません。これはCodePenのプロジェクトで発生します。私のコンピュータでは、見積もりを3回生成します(見積もり生成ボタンの最初の3回のクリックはうまくいきます)。 Safariではまったく動作しません。これはなぜでしょうか?Chromeデベロッパーツールを開いたときに何かが壊れる

私のJavaScriptでもリファクタリングを使用できると確信しています。ありがとう!

Link to CodePen Demo

HTML

<html> 
<head> 
    <title>Random Quote Generator</title> 
    <link rel="stylesheet" type="text/css" href="css/quote.css"> 
    <link href='https://fonts.googleapis.com/css?family=Lato:300italic,300' rel='stylesheet' type='text/css'> 



</head> 
<body> 

    <div class="quote-container"> 
     <div class="quote" id="msg"></div> 

    </div> 
    <div class="button-container"> 
     <a href="#" id="button">Get Quote</a> 
    </div> 
    <div id="twtbtn"></div> 




<script src="https://code.jquery.com/jquery-3.0.0.min.js" integrity="sha256-JmvOoLtYsmqlsWxa7mDSLMwa6dZ9rrIdtrrVYRnDRH0=" crossorigin="anonymous"></script> 
<script type="text/javascript" src="js/quote.js"></script> 
</body> 
</html> 

CSS

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; 
}; 

は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); 
      }); 
     }); 

} 

答えて

0

[OK]を...ので、私はその後、私ワットそれを理解しようと狂ったつもりでしたjquery用のgetJSONのドキュメントを参照してください。私はJSONP約100%ないですが、あなたはurl..it JSONPを使用するに&コールバックを追加するときに、事のthats。だから私はそれを削除し、それはサファリで素晴らしい動作します。ここで

FIXED

をcodepen、ここjqueryのからの引用です:?

コールバック= 『「URLが文字列が含まれている場合』(または類似した、サーバー側のAPIによって定義されます)リクエストはJSONPとして扱われます。詳細については、$ .ajax()のjsonpデータ型の説明を参照してください。

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

サファリはそれがで動作しませんでした唯一のものである理由は、残念ながら説明していません...しかし、ちょっと、それはそれを修正:)