我尝试将来自我的Twitter的Feed嵌入到我的网站中,并尝试将代码嵌入变量中。我想点击按钮时显示Feed,但是调用我的变量时遇到问题。是否可以将嵌入脚本放入变量
这里是我的代码
$(document).ready(function(){
var feed = "<a class='twitter-timeline' data-width='400' data-height='300' href='https://twitter.com/prabowo?ref_src=twsrc%5Etfw'>Tweets</a> <script async src='https://platform.twitter.com/widgets.js' charset='utf-8'></script>";
$(".show-feed").click(function(){
$(".my-feed").html(feed);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show-feed">
Show Twitter Feed
</div>
<div class="my-feed">
</div>
&#13;
答案 0 :(得分:0)
这是一种附加脚本和输入标记的方法。最好直接添加脚本并隐藏feed div,直到您单击它为止。另请查看jquery getScript函数。
$(document).ready(function(){
var scriptTag = document.createElement('script');
scriptTag.setAttribute('src','https://platform.twitter.com/widgets.js');
scriptTag.setAttribute('async','true');
scriptTag.setAttribute('charset','utf-8');
var link = document.createElement('a');
link.setAttribute('data-width', '400');
link.setAttribute('data-height','300');
link.setAttribute('href', 'https://twitter.com/prabowo?ref_src=twsrc%5Etfw');
link.textContent = 'Tweets';
$(".show-feed").click(function(){
$('.my-feed').append(scriptTag, link);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show-feed">
Show Twitter Feed
</div>
<div class="my-feed">
</div>