Uncaught TypeError: $(...).cytoscape is not a function

Uncaught TypeError: $(...).cytoscape is not a function

我在 Internet 上找到了这段代码并且它有效:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="~/Scripts/cytoscape.js"></script>

   <div id="cy"></div>
    <script>
       $(function(){ // on dom ready

$('#cy').cytoscape({
  style: cytoscape.stylesheet()
    .selector('node')
      .css({
        'content': 'data(deneme)'
      })

我将这段代码更改为下面的代码(我基本上将该代码块移到了 ajax 调用的成功部分):

  $("form").submit(function (event) {
        event.preventDefault();
        $.ajax({
            type: 'POST',
            url: "/MyController/MyAction",
           data: { nameid: $('#nameid').val() },
            success: function (newdata) {

                $('#cy').cytoscape({
                    layout: {

它给出了这个错误:

> MainGraph:55 Uncaught TypeError: $(...).cytoscape is not a function
>     at Object.success (MyCode:55)
>     at i (jquery.min.js:2)
>     at Object.fireWith [as resolveWith] (jquery.min.js:2)
>     at A (jquery.min.js:4)
>     at XMLHttpRequest.<anonymous> (jquery.min.js:4)

我怎样才能完成这项工作?我尝试将代码更改为:

var cy = cytoscape({
  container: $('#cy'),

上面的错误消失了,但在其他部分失败了。我该如何解决这种情况?谢谢

没有$(el).cytoscape()函数。你做 let cytoscape = require('cytoscape'); let cy = cytoscape();let Cytoscape = require('cytoscape'); let cy = new Cytoscape();]

通常不需要将 cy init 放在回调中。只需指定 elements 作为承诺,Cytoscape 会处理其余的事情。