【问题标题】:Execute Javascript-File with Button click in d3使用 d3 中的按钮单击执行 Javascript 文件
【发布时间】:2016-02-14 21:18:18
【问题描述】:

我在 node.js 中有一个 d3 图。在这个图表中,我正在调用

d3.select("#updatebutton").on("click", function(e) {
      //execute another js-file and reload the graph
}

我想做的是:单击更新按钮时,我想执行另一个 javascript 文件(wiki.js),然后重新加载图形(就像在 webbrowser 中重新加载一样)。

这可能吗?我会很高兴得到一些帮助! 谢谢!

【问题讨论】:

    标签: javascript node.js function d3.js execute


    【解决方案1】:

    您可以简单地在头部添加一个脚本元素:

    d3.select("#updatebutton").on("click", function(e) {
          //execute another js-file 
         var scriptElement = document.createElement('script');
         scriptElement.onload = function(){
            //and redraw the graph
         }
         scriptElement.src = "wiki.js";
         document.head.appendChild(scriptElement);
    })
    

    【讨论】:

    • 所以在我上面的函数中我只需调用 scriptElement.onload = function (){//refresh here} ?
    • 如果您想在点击时加载文件,您必须在函数中包含所有代码。在 onload 回调中可以重绘图形,如果文件被加载就会执行。
    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 2017-07-25
    • 2012-10-20
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多