最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Removing an element by ID (jointJS) - Stack Overflow

programmeradmin1浏览0评论

I noticed that JointJS links can be removed by hovering over them and clicking the big red X that appears. But I was wondering if it is possible remove an element once it has been created, without knowing the variable name.

onCreateButtonClick(function(){
  var rect = new joint.shapes.basic.Rect({
    position: { x: 100, y: 30 },
    size: { width: 100, height: 30 }
  });
  graph.addCell([rect]);
});

onRemoveButtonClick(function(){
   //removeRectangle here?
});

My question is: can I remove this rectangle in the second function?

I noticed that JointJS links can be removed by hovering over them and clicking the big red X that appears. But I was wondering if it is possible remove an element once it has been created, without knowing the variable name.

onCreateButtonClick(function(){
  var rect = new joint.shapes.basic.Rect({
    position: { x: 100, y: 30 },
    size: { width: 100, height: 30 }
  });
  graph.addCell([rect]);
});

onRemoveButtonClick(function(){
   //removeRectangle here?
});

My question is: can I remove this rectangle in the second function?

Share Improve this question asked Dec 11, 2013 at 23:11 mayorbyrnemayorbyrne 5071 gold badge5 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Removing elements by ID can simply be done as: graph.getCell(cellID).remove(). In your onRemoveButonClick(), you have to somehow know which element you want to remove. This depends on you application UI but you can, for example, do something like:

var selected;

paper.on('cell:pointerdown', function(cellView) {
    selected = cellView.model;
});

onRemoveButtonClick(function() { 
    if (selected) selected.remove(); 
});

I implemented the removal of an element by single clicking the element , using the cellView arguement directly.

paper.on('cell:pointerclick', function(cellView, evt, x, y) {
    cellView.remove();
});
发布评论

评论列表(0)

  1. 暂无评论