簡體   English   中英

無法將外部Javascript與D3JS鏈接

[英]Unable to link external Javascript with D3JS

請參閱“思想笨拙”: http ://plnkr.co/edit/2lvNHxvLEsNpVqNqmDUX? p= preview

我正在嘗試使用javascript使用D3JS畫一個圓。 我的script.js文件已定義了圓圈。

    d3.select("body")
  .append("svg")
    .attr("width",50)
    .attr("height",50)
  .append("circle")
    .attr("cx",25)
    .attr("cy",25)
    .attr("r",25)
    .style("fill","purple");

但它不會在D3圓圈標題后的html中顯示。 有人可以告訴我我在做什么錯嗎?

<!DOCTYPE html>
<html>

  <head>
    <script data-require="d3@*" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
    <title>D3JS Demo - Drawing Shapes</title>
  </head>

  <body>
    <h3>SVG Bar</h3>
    <svg>
      <rect width="50" height="200" style="fill:blue"></rect>
    </svg>

    <h3>D3 Bar</h3>
    <script>
      d3.select("body")
        .append("svg")
        .append("rect")
          .attr("widt ah",50)
          .attr("height",200)
            .style("fill","blue");
    </script>

    <h3>SVG Circle</h3>
    <svg width="50" height="50">
      <circle cx="25" cy="25" r="25" style="fill:blue"></circle>
    </svg>

    <h3>D3 Circle</h3>

  </body>

</html>

您需要在h3之后而不是head添加script ,如下所示:

<h3>D3 Circle</h3>
<script src="script.js"></script>

在執行script時,尚無body元素。 這意味着如果腳本在頭運行,則d3.select("body")將為空。

http://plnkr.co/edit/Xofci5Rm9XdpsP1zqu8a?p=preview

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM