【问题标题】:Cytoscape event target undefinedCytoscape 事件目标未定义
【发布时间】:2019-10-20 10:22:26
【问题描述】:

我整理了一个最小的工作 Cytoscape 示例。我可以显示图表,但我最感兴趣的是处理事件,这就是我遇到困难的地方。

问题是我不能访问单个元素的属性,而是可以看到任何事件的目标都是未定义的

下面是我的整个 html 文件。请注意 - 我只对这条线感兴趣

cy.nodes().bind('mouseover', (e) => console.log(e.target));

当我查看控制台时,每当我将鼠标指针移到节点上时,我只能看到“未定义”。正如我上面所说,我希望最终能够访问元素属性。

<!doctype html>
<html>
<head>
<title>Tutorial 1: Getting Started</title>
<script src="cytoscape.js"></script>
</head>

<style>
#cy {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0px;
    left: 0px;
}
</style>
<body>
<div id="cy"></div><script>
      var cy = cytoscape({
        container: document.getElementById('cy'),
        elements: [
      { data: { id: 'a' } },
      { data: { id: 'b' } },
      {
        data: {
          id: 'ab',
          source: 'a',
          target: 'b'
        }
      }]
  });

  cy.unbind("tap"); 

cy.bind("tap", "edge",function(evt) {
var tgt=this;
  alert(tgt);

});

cy.nodes().bind('mouseover', (e) => console.log(e.target));  


</script>
</body>
</html>

【问题讨论】:

    标签: events cytoscape.js


    【解决方案1】:

    您可以在下面的 sn-p 中看到一个工作示例,我真的只是更改了导入,没有别的:

    var cy = cytoscape({
      container: document.getElementById('cy'),
      elements: [{
          data: {
            id: 'a'
          }
        },
        {
          data: {
            id: 'b'
          }
        },
        {
          data: {
            id: 'ab',
            source: 'a',
            target: 'b'
          }
        }
      ]
    });
    
    cy.unbind("tap");
    
    cy.bind("click", "node, edge", function(evt) {
      var tgt = evt.target.data();
      console.log(tgt);
    
    });
    
    cy.nodes().bind('mouseover', (e) => console.log(e.target.data()));
    #cy {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0px;
      left: 0px;
    }
    <html>
    
    <head>
      <title>Tutorial 1: Getting Started</title>
      <script src="https://cdn.jsdelivr.net/npm/cytoscape@3.11.0/dist/cytoscape.min.js"></script>
    </head>
    
    <body>
      <div id="cy"></div>
    </body>
    
    </html>

    【讨论】:

    • 是的,您的示例当然有效。问题与脚本版本有关。你加入的那个没问题。我从其他地方保存在磁盘上的两个不同的导致各种问题。为什么会这样,我不知道。
    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多