您可以遍历每个 TextFlow 元素内的所有元素,并在每个元素上迭代其所有属性以创建等效的 html style 属性。像这样的……
var attrConversion = {
'textalign': ['text-align','',''], // [Style name, Value prefix, Value Suffix]
'color': ['color','',''],
'fontfamily': ['font-family','',''],
'fontsize': ['font-size','','px'],
'fontstyle': ['font-style','',''],
'fontweight': ['font-weight','','']
};
function attrConversor(textflowAttr) {
var attr = attrConversion[textflowAttr.name];
return attr[0] + ':' + attr[1] + textflowAttr.value + attr[2];
}
$('TextFlow *').each(function() {
var styleField = [], textflowAttrs = [];
var element = this;
// Loop through all attributes.
$.each(this.attributes,function() {
if (this.specified) {
textflowAttrs.push(this.name);
styleField.push(attrConversor(this));
}
});
// Remove all textflow attibutes from the element (you can't do it
// in the $.each because it would affect the iterations).
for (var i=0, l=textflowAttrs.length ; i<l; i++)
element.removeAttribute(textflowAttrs[i]);
// Apply the 'style' html attribute.
$(this).attr('style',styleField.join(';'));
});
这种方法有一些问题...
虽然看起来 TextFlow 属性名称与 html 一样,但采用驼峰式格式,但问题是 this.attributes 得到一个小写的 this.name,因此您必须创建一个对象将 textflow 属性名称转换为 html 版本。
某些 TextFlow 属性值可能与 html 样式版本有一些细微差别。例如,字体系列名称可能不同,fontSize 没有 px 后缀,因此您必须手动输入等等。您必须发现并处理所有这些内容。
这里有一个小提琴的例子...https://fiddle.jshell.net/rigobauer/xvukm9sn/
并不完美,但至少能给你一些可以使用的东西。希望对你有帮助