javascript - JointJS:无法使用 JSON 以自定义形状显示 foreignObject
问题描述
我正在尝试在 JointJS 中定义一个包含标签的自定义形状。如果我将标记定义为字符串,它就可以正常工作:
joint.shapes.examples.CustomRectangle = joint.shapes.standard.Rectangle.define('examples.CustomRectangle', {
markup: '<rect class="card"/><foreignObject x="10" y="10" height="60" width="100"><input xmlns="http://www.w3.org/1999/xhtml" type="text"></input></foreignObject>',
attrs: {
rect: {
refWidth: '100%',
refHeight: '100%',
strokeWidth: 5,
stroke: '#000000',
fill: 'white'
},
}
});
请参阅此代码笔并注意输入框在正方形内可见:http: //jsfiddle.net/dovrosenberg/kbmwfg1a/89/
如果我在 SVG 中定义了看起来相同但使用 JSON 的 XML:
joint.shapes.examples.CustomRectangle = joint.shapes.standard.Rectangle.define('examples.CustomRectangle', {
markup: [{
tagName: 'rect',
selector: 'rect'
},
{
tagName: 'foreignObject',
selector: 'fo',
attributes: {
x: '10',
y: '10',
width: '60',
height: '100',
},
children: [{
tagName: 'input',
selector: 'inpt',
attributes: {
xmlns: 'http://www.w3.org/1999/xhtml',
type: 'text',
},
}]
}
],
attrs: {
rect: {
refWidth: '100%',
refHeight: '100%',
strokeWidth: 5,
stroke: '#000000',
fill: 'white'
},
}
});
请参阅此代码笔并注意输入框不可见:http: //jsfiddle.net/dovrosenberg/asnvwe1r/4/
当我在浏览器中查看它时,生成的 SVG 看起来几乎相同。我能看到的唯一区别是 JSON 版本插入joint-selector
属性,但我认为这无关紧要。
更奇怪的是,如果您在调试器中编辑 HTML 并对 foreignObject 标记进行微小更改(例如更改关节选择器属性之一),则输入变为可见。
这不应该工作吗?
解决方案
感谢罗伯特朗森的精彩评论,我能够弄清楚。正确的标记如下(或参见http://jsfiddle.net/dovrosenberg/asnvwe1r/8/)。不同之处在于新namespaceURI
密钥。
joint.shapes.examples.CustomRectangle = joint.shapes.standard.Rectangle.define('examples.CustomRectangle', {
markup: [{
tagName: 'rect',
selector: 'rect'
},
{
tagName: 'foreignObject',
selector: 'fo',
attributes: {
x: '10',
y: '10',
width: '60',
height: '100',
},
children: [{
tagName: 'input',
selector: 'inpt',
namespaceURI: 'http://www.w3.org/1999/xhtml',
attributes: {
type: 'text',
},
}]
}
],
attrs: {
rect: {
refWidth: '100%',
refHeight: '100%',
strokeWidth: 5,
stroke: '#000000',
fill: 'white'
},
}
});
推荐阅读
- python - Signxml 验证失败
- powershell - 如何使用 Powershell 在集群中创建共享?
- mysql - 从数据库中删除所有记录而不更改或删除一致性约束
- stripe-payments - 我可以为订阅提供不同的送货/账单地址吗?
- php - 如何使用 mysqli 编写具有可变数量的用户提供的值的安全 SELECT 查询?
- ruby-on-rails - api保存多个子记录只保存一个缺少字段ruby
- javascript - 只允许数字,十进制值最多两位,删除和退格
- javascript - Firebase 身份验证,如何检测页面是重定向结果(或重定向到特定 url)?
- android - How to detect tablet or smartphone using tablet()?
- kendo-ui - 删除剑道网格中的记录时不显示进度条