javascript 最简单的 D3.js 示例不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16670806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simplest D3.js example not working
提问by explunit
I have this in my index.htmlfile but it does not show the paragraph that I want to add with D3
我的index.html文件中有这个,但它没有显示我想添加的段落D3
<!DOCTYPE html>
<html>
<head>
<title> D3 page template </title>
<script type="text/javascript" src = "d3/d3.v3.js"></script>
</head>
<body>
<script type="text/javascript">
d3.select("body").append("p").text("new paragraph!");
</script>
</body>
</html>
The path to where I am referencing D3.jsshould be correct because if I do an inspect element in the browser I can click on the D3link and it takes me to its source code.
我所引用的路径D3.js应该是正确的,因为如果我在浏览器中执行一个检查元素,我可以单击该D3链接并将我带到其源代码。


回答by explunit
You are missing the character set in your HTML page. Add something like this:
您缺少 HTML 页面中的字符集。添加如下内容:
<meta charset="utf-8">
The un-minified source of D3 includes the actual symbol for pi, which confuses the browser if the character set is not defined.
D3 的未缩小源包括 pi 的实际符号,如果未定义字符集,它会混淆浏览器。
回答by Frison Alexander
I am going to assume that you are testing this out without a web server. If so, then your URL will read file://.... not http://..
我将假设您在没有 Web 服务器的情况下进行测试。如果是这样,那么您的 URL 将读取 file://.... 而不是 http://..
With this, the Javascript request will go to file:///.../D3/d3/d3.v3.js which won't have the proper response header set, such as charset and MIME.
这样,Javascript 请求将转到 file:///.../D3/d3/d3.v3.js,它不会设置正确的响应标头,例如字符集和 MIME。
You can always get it from a CDN to avoid this problem:
您始终可以从 CDN 获取它以避免此问题:
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>

