如何更改自定义模板 JointJS 元素的 CSS?

How to Change CSS of Custom Template JointJS Element?

我已经使用 JointJS 一段时间了,我正在尝试为我的元素创建一个 HTML 模板。所以我一直在使用 tutorial 但它并没有停止为我做这件事。 我想要完成的事情是在执行某个操作时调整 HTML 元素的颜色,例如双击该元素。我确实注意到教程中文本的更改方式,但是没有更改任何颜色的示例。

编辑

我试过这个来获得元素的起始颜色:

joint.shapes.html = {};
joint.shapes.html.OdinElement = joint.shapes.basic.Rect.extend({
  defaults: joint.util.deepSupplement({
    type: 'html.Element',
    attrs: {
      rect: { stroke: 'none', 'fill-opacity': 0 }
    }
  }, joint.shapes.basic.Rect.prototype.defaults)
});

// Create a custom view for that element that displays an HTML div above it.
// -------------------------------------------------------------------------

joint.shapes.html.OdinElementView = joint.dia.ElementView.extend({

  template: [
    '<div class="html-element">',
    '<button class="delete">x</button>',
    '<label></label>',
    '<span></span>', '<br/>',
    '</div>'
  ].join(''),

  initialize: function() {
    _.bindAll(this, 'updateBox');
    joint.dia.ElementView.prototype.initialize.apply(this, arguments);

    this.$box = $(_.template(this.template)());

    this.$box.find('.delete').on('click', _.bind(this.model.remove, this.model));
    // Update the box position whenever the underlying model changes.
    this.model.on('change', this.updateBox, this);
    // Remove the box when the model gets removed from the graph.
    this.model.on('remove', this.removeBox, this);

    this.updateBox();
  },
  render: function() {
    joint.dia.ElementView.prototype.render.apply(this, arguments);
    this.paper.$el.prepend(this.$box);
    this.updateBox();
    return this;
  },
  updateBox: function() {
    // Set the position and dimension of the box so that it covers the JointJS element.
    var bbox = this.model.getBBox();
    // Example of updating the HTML with a data stored in the cell model.
    this.$box.find('label').text(this.model.get('label'));

    this.$box.css({ width: bbox.width, height: bbox.height, left: bbox.x, top: bbox.y, transform: 'rotate(' + (this.model.get('angle') || 0) + 'deg)', background: this.model.get('color')}); // I've tried to add it like a background
  },
  removeBox: function(evt) {
    this.$box.remove();
  }
});

//add a new element like this
new joint.shapes.html.OdinElement({
    position: { x: 80, y: 80 },
    size: { width: 200, height: 50 },
    label: 'label',
    color: '#ff0000'
  });

我也试过像在标签中设置文本一样设置它,但我不知道是否有这样的功能。

有人知道怎么做吗?

非常感谢!

蒂姆

我自己找到了答案!

显然不允许在 html 变量中创建与给定元素不同的元素。所以我不得不将 joint.shapes.html.OdinElement 更改为 joint.shapes.html.Element 并将 joint.shapes.html.OdinElementView 更改为 joint.shapes.html.ElementView

现在一切正常:)

感谢您的帮助!