Cytoscape: TypeError 即使有教程
Cytoscape: TypeError even with the tutorial
我想使用 Cytoscape.js, so I copy-pasted their tutorial:
var cy = cytoscape({
container: document.getElementById('cy')
});
...我得到了这个(在 Firefox 控制台上):
TypeError: r is null
和这个(在 Safari 控制台上):
TypeError: null is not an object (evaluating 'r.className')
错误在 Chrome 上仍然存在,始终显示 cytoscape.min.js
的第 39 行。
当然,我有一个HTML <div id="cy"></div>
元素。
为什么会出现此错误?另外,我尝试添加一些节点,它们在 JSFiddle 上呈现,但在浏览器上不呈现...
错误原因如下:我试图在加载页面之前初始化 Cytoscape 图,因此 Cytoscape 找不到任何 #cy
div 在 HTML.
里面
解决方案是将初始化包装在一个函数中,并在页面加载后调用该函数(<body onload="setupMyGraph();"> ... </body>
)。
afaik,这应该可以解决您的问题
document.addEventListener("DOMContentLoaded", function() {
cy = cytoscape({
container: document.getElementById("cy"),
})
<cytoscape stuff like adding nodes, setting style, ...>
})
我想使用 Cytoscape.js, so I copy-pasted their tutorial:
var cy = cytoscape({
container: document.getElementById('cy')
});
...我得到了这个(在 Firefox 控制台上):
TypeError: r is null
和这个(在 Safari 控制台上):
TypeError: null is not an object (evaluating 'r.className')
错误在 Chrome 上仍然存在,始终显示 cytoscape.min.js
的第 39 行。
当然,我有一个HTML <div id="cy"></div>
元素。
为什么会出现此错误?另外,我尝试添加一些节点,它们在 JSFiddle 上呈现,但在浏览器上不呈现...
错误原因如下:我试图在加载页面之前初始化 Cytoscape 图,因此 Cytoscape 找不到任何 #cy
div 在 HTML.
解决方案是将初始化包装在一个函数中,并在页面加载后调用该函数(<body onload="setupMyGraph();"> ... </body>
)。
afaik,这应该可以解决您的问题
document.addEventListener("DOMContentLoaded", function() {
cy = cytoscape({
container: document.getElementById("cy"),
})
<cytoscape stuff like adding nodes, setting style, ...>
})