我正在尝试通过 this method 将推文嵌入到我的网站中.
但我无法做到正确。这是我的 HTML 标记:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<blockquote class="twitter-tweet" lang="en">
<p lang="en" dir="ltr">Can you answer this? Tem como calcular a diagonal no Canvas? <a href="http://t_.co/E8dD01jGKX">http://t_.co/E8dD01jGKX</a> <a href="https://twitter.com/hashtag/javascript?src=hash">#javascript</a></p>
— SO em Português (@StackOverflowPT) <a href="https://twitter.com/StackOverflowPT/status/613791039539806208">June 24, 2015</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
</body>
</html>
我在浏览器中看到的是:
只是 HTML,而不是 Twitter 嵌入推文框的样式。
最佳答案
到 Twitter 的 widgets.js
的链接是所谓的相对协议(protocol)或无方案:它没有定义自己的 URI scheme并使用其主机页面恰好打开的任何内容。这对于支持 http
和 https
方案非常有用,而无需更改源代码中的链接。
现在,因为您正在使用本地文件进行测试,所以使用的方案是 file
,即 specifically created对于与浏览器位于同一台计算机上的文件。因此无协议(protocol) //platform.twitter.com/widgets.js
获取 file
方案并成为本地文件 widgets.js
的链接code> 位于 platform.twitter.com
文件夹中,您的计算机上可能没有该文件夹。
要解决这个问题,只需添加一个方案,http
或 https
,如下所示:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<blockquote class="twitter-tweet" lang="en">
<p lang="en" dir="ltr">Can you answer this? Tem como calcular a diagonal no Canvas? <a href="http://t.co/E8dD01jGKX">http://t.co/E8dD01jGKX</a> <a href="https://twitter.com/hashtag/javascript?src=hash">#javascript</a>
</p>
— SO em Português (@StackOverflowPT) <a href="https://twitter.com/StackOverflowPT/status/613791039539806208">June 24, 2015</a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<!-- ^ -->
</body>
</html>
关于javascript - 无法将推文嵌入 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31036649/