如何将onclick事件添加到joint.js元素?

aha*_*ert 12 javascript jointjs

我在DAG中有一个joint.js元素,并希望能够通过单击它来触发事件.

我可以使用$(selector).click(...)它,但我想知道是否有一个joint.js特定的处理方式,因为这可能会更好.我决定的一个事件是onclick的候选人是'批处理:停止'

我的代码:

 var variable =  new joint.shapes.basic.Rect({
     name : label,
     id: label,
     onclick : function () {alert("hello");},
     size: { width: width, height: height },
     attrs: {
         text: { text: label, 'font-size': letterSize, 'font-family': 'monospace' },
         rect: {
             fill : fillColor, 
             width: width, height: height,
             rx: 5, ry: 5,
             stroke: '#555'
         }   
     }   
 }); 
 variable.on('batch:stop', function (element) {alert(""); toggleEvidence(element.name);});
 return variable;
Run Code Online (Sandbox Code Playgroud)

我应该如何添加onclick事件?

dav*_*ave 23

JointJS形状是模型,因此您点击处理程序无法使用它们是正确的.JointJS论文触发了可能对您有用的事件:

paper.on('cell:pointerdown', 
    function(cellView, evt, x, y) { 
        alert('cell view ' + cellView.model.id + ' was clicked'); 
    }
);
Run Code Online (Sandbox Code Playgroud)

其他事件有:cell:pointerup,cell:pointerdblclick,cell:pointermove.

完整列表可以在这里找到:http://jointjs.com/api#joint.dia.Paper : events.

编辑:

从JointJS v0.9开始,还有一个cell:pointerclick事件.

  • 我试图在单击jointjs元素内的html链接时触发一个函数.怎么办? (2认同)