您可以遍历每个TextFlow元素内部和每个元素上的所有元素,迭代其所有属性以创建等效的html style
属性。事情是这样的......
var attrConversion = {
'textalign': ['text-align','',''], // [Style name, Value preffix, 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/
并不完美,但至少给你一些帮助。我希望它有帮助
你用'element.getAttribute('fontFamily')得到的值' – LGSon