2017-08-01 96 views
1

我有一个jQuery脚本附加Instagram blockquote,但它不适用于第二个附加。第一次被追加时,blockquote会像它应该那样转换到iframe,但是如果我再次尝试,它只会保留一个blockquote。jquery附加Instagram的blockquote嵌入动态不起作用

function appendInstagram($controls){ 
    var instagramURL = $('#js-instagram-url-edit').val(); 
    var instagramScript = '<script async defer src="//platform.instagram.com/en_US/embeds.js"><\/script>' 
    var instagramHtml = $('<blockquote class="instagram-media" 
         data-instgrm-captioned data-instgrm-version="7"> 
          <div style="padding:8px;"> 
          <a href="'+instagramURL+'" target="_blank"></a> 
          </div> 
         </blockquote>' + instagramScript); 
    updateContent(instagramHtml, $controls); 
} 

的想法是,当这个功能被excecuted,可以将blockquote获得通过updateContent()

回答

0

,你有它的编码要追加的JavaScript代码就不会被执行追加。我嘲笑起来的东西,应该有所帮助:

<html> 
<head><title>test</title> 

<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> 

<script> 
function appendInstagram($controls){ 
    var instagramURL = $("#url").val(); 

    var instagramHtml = '<blockquote class="instagram-media" data-instgrm-captioned data-instgrm-version="7">' + 
          '<div style="padding:8px;"><a href="'+instagramURL+'" target="_blank"></a></div>' + 
         '</blockquote>'; // + instagramScript; 

    $("#test").html(instagramHtml); 
} 

$(document).ready(function() { 
    appendInstagram(null); 

    var s = document.createElement("script"); 
    s.type = "text/javascript"; 
    s.src = "https://platform.instagram.com/en_US/embeds.js"; 
    s.async = true; 
    $("#test").append(s); 
}); 
</script> 
</head> 
<body> 
<div id="test"> 

</div> 
<input type="text" id="url" value="https://www.instagram.com/p/BW52LY1FMdf/?taken-by=birminghamfencingclub_bfc" /> 

</body> 
</html> 

欲了解更多信息,看看这里:How to Append <script></script> in javascript?

1

我发现Instagram的有这个

window.instgrm.Embeds.process() 

需要加以执行每一次。

+0

它的作品,但如何?这是怎么回事? –