我的javascript组件未呈现,并且出现以下错误:

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


这是引发错误的代码。

本质上,我正在尝试隔离JavaScript渲染到行内容的内容。当我运行此程序而不将其嵌套在容器中时,它可以正常工作。



<!DOCTYPE html>
<html>
  <head>

    <title>Main Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/static/css/graph4.css">


    <!--[if lt IE 9]>
          <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
          <script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->


  </head>
  <body>


<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
       <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
  <div class="container-fluid">


  <div class="row">
    <div class="col-xs-12 col-sm-12 col-md-12" style="background-color:lavender;">
	  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
	  <script src="http://cytoscape.github.io/cytoscape.js/api/cytoscape.js-latest/cytoscape.min.js"></script>
          <script>
		$(function(){ // on dom ready

		$('#cy').cytoscape({
		    layout: {
    		    name: 'cose',
		    padding: 10,
		    randomize: true
		  },

		  style: cytoscape.stylesheet()
		    .selector('node')
		      .css({
		        'shape': 'data(faveShape)',
		        'width': 'mapData(weight, 40, 80, 20, 60)',
		        'content': 'data(name)',
		        'text-valign': 'center',
		        'text-outline-width': 2,
		        'text-outline-color': 'data(faveColor)',
		        'background-color': 'data(faveColor)',
		        'color': '#fff'
		      })
		    .selector(':selected')
		      .css({
		        'border-width': 3,
		        'border-color': '#333'
		      })
		    .selector('edge')
		      .css({
		        'curve-style': 'bezier',
		        'opacity': 0.666,
		        'width': 'mapData(strength, 70, 100, 2, 6)',
		        'target-arrow-shape': 'triangle',
		        'source-arrow-shape': 'circle',
		        'line-color': 'data(faveColor)',
		        'source-arrow-color': 'data(faveColor)',
		        'target-arrow-color': 'data(faveColor)'
		      })
		    .selector('edge.questionable')
		      .css({
		        'line-style': 'dotted',
		        'target-arrow-shape': 'diamond'
		      })
		    .selector('.faded')
		      .css({
		        'opacity': 0.25,
		        'text-opacity': 0
     		 }),

		  elements: {
		    nodes: [
		      { data: { id: 'j', name: 'Jerry', weight: 65, faveColor: '#6FB1FC', faveShape: 'triangle' } },
		      { data: { id: 'e', name: 'Elaine', weight: 45, faveColor: '#EDA1ED', faveShape: 'ellipse' } },
		      { data: { id: 'k', name: 'Kramer', weight: 75, faveColor: '#86B342', faveShape: 'octagon' } },
		      { data: { id: 'g', name: 'George', weight: 70, faveColor: '#F5A45D', faveShape: 'rectangle' } }
		    ],
		    edges: [
		      { data: { source: 'j', target: 'e', faveColor: '#6FB1FC', strength: 90 } },
		      { data: { source: 'j', target: 'k', faveColor: '#6FB1FC', strength: 70 } },
		      { data: { source: 'j', target: 'g', faveColor: '#6FB1FC', strength: 80 } },

		      { data: { source: 'e', target: 'j', faveColor: '#EDA1ED', strength: 95 } },
		      { data: { source: 'e', target: 'k', faveColor: '#EDA1ED', strength: 60 }, classes: 'questionable' },

		      { data: { source: 'k', target: 'j', faveColor: '#86B342', strength: 100 } },
		      { data: { source: 'k', target: 'e', faveColor: '#86B342', strength: 100 } },
		      { data: { source: 'k', target: 'g', faveColor: '#86B342', strength: 100 } },

		      { data: { source: 'g', target: 'j', faveColor: '#F5A45D', strength: 90 } }
		    ]
		  },

		  ready: function(){
		    window.cy = this;

		    // giddy up
		  }
		});

		}); // on dom ready








		</script>
          <div id="cy"></div>
	  <p>Test</p>
    </div>
    <div class="col-xs-12 col-sm-12 col-md-12" style="background-color:lavenderblush";>
        Timeline
    </div>
  </div>













</div>

<footer class="footer">
<p>
<center>
<span class="glyphicon glyphicon-copyright-mark" aria-hidden="true"></span>
Test
</center>
</p>
</footer>


    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  </body>
</html>

最佳答案

$('#cy').cytoscape({
  layout: {
    name: 'cose',
    padding: 10,
    randomize: true
  },
  ...
};


需要更改为

var cy = cytoscape({
  container: $('#cy'),
  layout: {
      name: 'cose',
    padding: 10,
    randomize: true
  },
  ...
};


按照http://js.cytoscape.org/#getting-started/initialisation上的说明

这将解决您的错误,但是我不确定它是否正在渲染您希望的样子。我希望这至少可以使您距离期望的行为更近一步!

10-06 15:28