Javascript 如何运行<;脚本>;从一个按钮

Javascript 如何运行<;脚本>;从一个按钮,javascript,d3.js,Javascript,D3.js,我是新的网页设计,我正在使用javascript库绘制一棵树。我的问题是,它是在加载页面时自动绘制的。我想有一个按钮,并在绘画上点击,但我找不到如何做到这一点。尝试将函数传递给javascript文件,但当传递它时,我不知道在html文件中如何调用它 这是小提琴 变量treeData=[ { “名称”:“顶级”, “父项”:“null”, “儿童”:[ { “名称”:“2级:A”, “父级”:“顶级”, “儿童”:[ { “姓名”:“人子”, “父级”:“级别2:A” }, { “姓名”:“A

我是新的网页设计,我正在使用javascript库绘制一棵树。我的问题是,它是在加载页面时自动绘制的。我想有一个按钮,并在绘画上点击,但我找不到如何做到这一点。尝试将函数传递给javascript文件,但当传递它时,我不知道在html文件中如何调用它

这是小提琴


变量treeData=[
{
“名称”:“顶级”,
“父项”:“null”,
“儿童”:[
{
“名称”:“2级:A”,
“父级”:“顶级”,
“儿童”:[
{
“姓名”:“人子”,
“父级”:“级别2:A”
},
{
“姓名”:“A的女儿”,
“父级”:“级别2:A”
}
]
},
{
“名称”:“2级:B级”,
“父级”:“顶级”
}
]
}
];
//*************生成树形图*****************
var margin={顶部:40,右侧:120,底部:20,左侧:120},
宽度=960-margin.right-margin.left,
高度=500-margin.top-margin.bottom;
var i=0;
var tree=d3.layout.tree().size([height,width]);
var diagonal=d3.svg.diagonal().projection(函数(d){return[d.x,d.y];});
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。右侧+边距。左侧)
.attr(“高度”,高度+边距。顶部+边距。底部)。追加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
根=树数据[0];
更新(根);
函数更新(源){
//计算新的树布局。
var nodes=tree.nodes(root).reverse(),
链接=树。链接(节点);
//为固定深度的.nodes.forEach(函数(d){d.y=d.depth*100;})进行规范化;
//声明节点…
var node=svg.selectAll(“g.node”).data(节点,函数(d){returnd.id | |(d.id=++i)});
//输入节点。
var nodeEnter=node.enter().append(“g”).attr(“类”、“节点”)
.attr(“转换”,函数(d){
返回“translate”(“+d.x+”,“+d.y+”);});
nodeEnter.append(“circle”).attr(“r”,10).style(“fill”,“#fff”);
nodeEnter.append(“text”).attr(“y”,函数(d){
返回d.children | | d.| U children?-18:18;})
.attr(“dy”,“.35em”)
.attr(“文本锚定”、“中间”)
.text(函数(d){返回d.name;})
.样式(“填充不透明度”,1);
//声明链接…
var link=svg.selectAll(“path.link”)
.data(链接,函数(d){返回d.target.id;});
//输入链接。
link.enter()插入(“路径”,“g”)
.attr(“类”、“链接”)
.attr(“d”,对角线);
}

调用按钮的最简单方法是

<button onclick="update()">Update</button>
更新

调用按钮的最简单方法是

<button onclick="update()">Update</button>
更新

您可以使用Javascript函数包装代码,并使用按钮的
onclick
属性启动代码

检查下面的代码,看看它是如何工作的

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 3px;
}

.node text { font: 12px sans-serif; }

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 2px;
}
</style>
</head>
<body>

<!-- load the d3.js library --> 
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

function renderTree() {

var treeData = [
 {
"name": "Top Level",
"parent": "null",
"children": [
  {
    "name": "Level 2: A",
    "parent": "Top Level",
    "children": [
      {
        "name": "Son of A",
        "parent": "Level 2: A"
      },
      {
        "name": "Daughter of A",
        "parent": "Level 2: A"
      }
    ]
  },
  {
    "name": "Level 2: B",
    "parent": "Top Level"
  }
]
}
];
// ************** Generate the tree diagram  *****************
var margin = {top: 40, right: 120, bottom: 20, left: 120},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree().size([height, width]);
var diagonal = d3.svg.diagonal().projection(function(d) { return [d.x, d.y]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom).append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
 // Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.nodes.forEach(function(d) { d.y = d.depth * 100; });
// Declare the nodes…
var node = svg.selectAll("g.node").data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g").attr("class", "node")
.attr("transform", function(d) { 
 return "translate(" + d.x + "," + d.y + ")"; });
nodeEnter.append("circle").attr("r", 10).style("fill", "#fff");
nodeEnter.append("text").attr("y", function(d) { 
      return d.children || d._children ? -18 : 18; })
  .attr("dy", ".35em")
  .attr("text-anchor", "middle")
  .text(function(d) { return d.name; })
  .style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
 // Enter the links.
 link.enter().insert("path", "g")
  .attr("class", "link")
  .attr("d", diagonal);
}

}
</script>
<input type="button" value="Render" onclick="renderTree();">
</body>
</html>

可折叠树示例
.节点圆{
填充:#fff;
笔画:钢蓝;
笔画宽度:3px;
}
.node文本{font:12px无衬线;}
.链接{
填充:无;
冲程:#ccc;
笔画宽度:2px;
}
函数renderTree(){
变量treeData=[
{
“名称”:“顶级”,
“父项”:“null”,
“儿童”:[
{
“名称”:“2级:A”,
“父级”:“顶级”,
“儿童”:[
{
“姓名”:“人子”,
“父级”:“级别2:A”
},
{
“姓名”:“A的女儿”,
“父级”:“级别2:A”
}
]
},
{
“名称”:“2级:B级”,
“父级”:“顶级”
}
]
}
];
//*************生成树形图*****************
var margin={顶部:40,右侧:120,底部:20,左侧:120},
宽度=960-margin.right-margin.left,
高度=500-margin.top-margin.bottom;
var i=0;
var tree=d3.layout.tree().size([height,width]);
var diagonal=d3.svg.diagonal().projection(函数(d){return[d.x,d.y];});
var svg=d3.选择(“正文”).追加(“svg”)
.attr(“宽度”,宽度+边距。右侧+边距。左侧)
.attr(“高度”,高度+边距。顶部+边距。底部)。追加(“g”)
.attr(“转换”、“平移”(+margin.left+)、“+margin.top+”);
根=树数据[0];
更新(根);
函数更新(源){
//计算新的树布局。
var nodes=tree.nodes(root).reverse(),
链接=树。链接(节点);
//为固定深度的.nodes.forEach(函数(d){d.y=d.depth*100;})进行规范化;
//声明节点…
var node=svg.selectAll(“g.node”).data(节点,函数(d){returnd.id | |(d.id=++i)});
//输入节点。
var nodeEnter=node.enter().append(“g”).attr(“类”、“节点”)
.attr(“转换”,函数(d){
返回“translate”(“+d.x+”,“+d.y+”);});
nodeEnter.append(“circle”).attr(“r”,10).style(“fill”,“#fff”);
nodeEnter.append(“text”).attr(“y”,函数(d){
返回d.children | | d.| U children?-18:18;})
.attr(“dy”,“.35em”)
.attr(“文本锚定”、“中间”)
.text(函数(d){返回d.name;})
.样式(“填充不透明度”,1);
//声明链接…
var link=svg.selectAll(“path.link”)
.data(链接,函数(d){返回d.target.id;});
//输入链接。
link.enter()插入(“路径”,“g”)
.attr(“类”、“链接”)
.attr(“d”,对角线);
}
}

您可以使用Javascript函数包装代码,并使用按钮的
onclick
属性启动代码

检查下面的代码,看看它是如何工作的

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 3px;
}

.node text { font: 12px sans-serif; }

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 2px;
}
</style>
</head>
<body>

<!-- load the d3.js library --> 
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

function renderTree() {

var treeData = [
 {
"name": "Top Level",
"parent": "null",
"children": [
  {
    "name": "Level 2: A",
    "parent": "Top Level",
    "children": [
      {
        "name": "Son of A",
        "parent": "Level 2: A"
      },
      {
        "name": "Daughter of A",
        "parent": "Level 2: A"
      }
    ]
  },
  {
    "name": "Level 2: B",
    "parent": "Top Level"
  }
]
}
];
// ************** Generate the tree diagram  *****************
var margin = {top: 40, right: 120, bottom: 20, left: 120},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree().size([height, width]);
var diagonal = d3.svg.diagonal().projection(function(d) { return [d.x, d.y]; });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom).append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
 // Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.nodes.forEach(function(d) { d.y = d.depth * 100; });
// Declare the nodes…
var node = svg.selectAll("g.node").data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g").attr("class", "node")
.attr("transform", function(d) { 
 return "translate(" + d.x + "," + d.y + ")"; });
nodeEnter.append("circle").attr("r", 10).style("fill", "#fff");
nodeEnter.append("text").attr("y", function(d) { 
      return d.children || d._children ? -18 : 18; })
  .attr("dy", ".35em")
  .attr("text-anchor", "middle")
  .text(function(d) { return d.name; })
  .style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
 // Enter the links.
 link.enter().insert("path", "g")
  .attr("class", "link")
  .attr("d", diagonal);
}

}
</script>
<input type="button" value="Render" onclick="renderTree();">
</body>
</html>

可折叠树示例
.节点圆{
填充:#fff;
笔画:钢蓝;
笔画宽度:3px;
}
.node文本{font:12px无衬线;}
.链接{
填充:无;
冲程:#ccc;
笔画宽度:2px;
}
函数renderTree(){
变量treeData=[
{
“名称”:“顶级”,
“父项”:“null”,
“儿童”:[
{
“名称”:“2级:A”,
“父级”:“顶级”,
“儿童”:[
{
“姓名”:“人子”,
“父级”:“级别2:A”
},
{
“姓名”:“A的女儿”,
“家长
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
.tree {
margin-bottom: 10px;
overflow: auto;
}
function go(){
var treeData =
  {
      "name": "Top Level",
      "parent": "null",
      "children": [
        {
            "name": "Level 2: A",
            "parent": "Top Level",
            "children": [
              {
                  "name": "Son of A",
                  "parent": "Level 2: A",
                   "children": []
              },
              {
                  "name": "Daughter of A",
                  "parent": "Level 2: A",
                  "children": []
              }
            ]
        },
        {
            "name": "Level 2: B",
            "parent": "Top Level",
            "children": []
        }
      ]
  };
BuildVerticaLTree(treeData,"#tree");
}
function BuildVerticaLTree(treeData, treeContainerDom)      {
  var margin = {
    top: 40,
    right: 120,
    bottom: 20,
    left: 120
  };
  var width = 960 - margin.right - margin.left;
  var height = 960 - margin.top - margin.bottom;

  var i = 0,
    duration = 750;
  var tree = d3.layout.tree()
    .size([height, width]);
  var diagonal = d3.svg.diagonal()
    .projection(function(d) {
      return [d.x, d.y];
    });
  var svg = d3.select(treeContainerDom).append("svg")
    .attr("width", width + margin.right + margin.left)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  root = treeData;

  update(root);

  function update(source) {
    // Compute the new tree layout.
    var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);
    // Normalize for fixed-depth.
    nodes.forEach(function(d) {
      d.y = d.depth * 100;
    });
    // Declare the nodes…
    var node = svg.selectAll("g.node")
      .data(nodes, function(d) {
        return d.id || (d.id = ++i);
      });
    // Enter the nodes.
    var nodeEnter = node.enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) {
        return "translate(" + source.x0 + "," + source.y0 + ")";
      }).on("click", nodeclick);
    nodeEnter.append("circle")
      .attr("r", 10)
      .attr("stroke", function(d) {
        return d.children || d._children ? "steelblue" : "#00c13f";
      })
      .style("fill", function(d) {
        return d.children || d._children ? "lightsteelblue" : "#fff";
      });
    //.attr("r", 10)
    //.style("fill", "#fff");
    nodeEnter.append("text")
      .attr("y", function(d) {
        return d.children || d._children ? -18 : 18;
      })
      .attr("dy", ".35em")
      .attr("text-anchor", "middle")
      .text(function(d) {
        return d.name;
      })
      .style("fill-opacity", 1e-6);
    // Transition nodes to their new position.
    //horizontal tree
    var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) {
        return "translate(" + d.x + "," + d.y + ")";
      });
    nodeUpdate.select("circle")
      .attr("r", 10)
      .style("fill", function(d) {
        return d._children ? "lightsteelblue" : "#fff";
      });
    nodeUpdate.select("text")
      .style("fill-opacity", 1);


    // Transition exiting nodes to the parent's new position.
    var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) {
        return "translate(" + source.x + "," + source.y + ")";
      })
      .remove();
    nodeExit.select("circle")
      .attr("r", 1e-6);
    nodeExit.select("text")
      .style("fill-opacity", 1e-6);
    // Update the links…
    // Declare the links…
    var link = svg.selectAll("path.link")
      .data(links, function(d) {
        return d.target.id;
      });
    // Enter the links.
    link.enter().insert("path", "g")
      .attr("class", "link")

    .attr("d", function(d) {
      var o = {
        x: source.x0,
        y: source.y0
      };
      return diagonal({
        source: o,
        target: o
      });
    });
    // Transition links to their new position.
    link.transition()
      .duration(duration)
      .attr("d", diagonal);


    // Transition exiting nodes to the parent's new position.
    link.exit().transition()
      .duration(duration)
      .attr("d", function(d) {
        var o = {
          x: source.x,
          y: source.y
        };
        return diagonal({
          source: o,
          target: o
        });
      })
      .remove();

    // Stash the old positions for transition.
    nodes.forEach(function(d) {
      d.x0 = d.x;
      d.y0 = d.y;
    });
  }

  // Toggle children on click.
  function nodeclick(d) {
    if (d.children) {
      d._children = d.children;
      d.children = null;
    } else {
      d.children = d._children;
      d._children = null;
    }
    update(d);
  }
}