new Tag(name: string | number = '', attr = {}, child = []);
let TAG=new TagUtil();
parseJSON(params: string):Tag//将json字符串转Tag对象
toJSON(params: Tag):string//将Tag对象转json字符串
setRule(params: object)//设置当前工具类的生成规则
getRule():object//获取当前工具类的生成规则
默认规则为
{
'@': {
'#': {
'tag': {
'*': ['<!-- ', TAG_RULE_child, ' -->'],
},
'attr': {
'*': [' ', TAG_RULE_attr_name, '="', TAG_RULE_attr_value, '"']
}
},
'style': {
'tag': {
'*': [TAG_RULE_tag, '{', TAG_RULE_attr, '}'],
},
'attr': {
'*': [' ', TAG_RULE_attr_name, ': ', TAG_RULE_attr_value, ';']
}
},
'tag': {
'br': ['<', TAG_RULE_tag, '/>'],
'hr': ['<', TAG_RULE_tag, TAG_RULE_attr, '/>'],
'link': ['<', TAG_RULE_tag, TAG_RULE_attr, '/>'],
'img': ['<', TAG_RULE_tag, TAG_RULE_attr, '/>'],
'input': ['<', TAG_RULE_tag, TAG_RULE_attr, '/>'],
'meta': ['<', TAG_RULE_tag, TAG_RULE_attr, '/>'],
'#': ['<!-- ', TAG_RULE_child, ' -->'],
'*': ['<', TAG_RULE_tag, TAG_RULE_attr, '>', TAG_RULE_child, '</', TAG_RULE_tag, '>'],
},
'attr': {
'*': [' ', TAG_RULE_attr_name, '="', TAG_RULE_attr_value, '"']
}
},
}
其中@代表当前级别,*代表任意,可通过修改TAG_SELECT_root和TAG_SELECT_all常量进行自定义
let TAG = new TagUtil();
let project = new Tag('html', { 'lang': 'zh' }, [
new Tag('head', {}, [
new Tag('title', {}, ['Demo']),
new Tag('meta', { 'charset': 'UTF-8' }),
new Tag('meta', { 'http-equiv': 'X-UA-Compatible', 'content': 'IE=edge' }),
new Tag('meta', { 'name': 'viewport', 'content': 'width=device-width, initial-scale=1.0' }),
]),
new Tag('body', {}, [
new Tag('h1', { id: 'hello' }, ['hello world!']),
new Tag('css', {}, [
new Tag('css/#hello', { 'font-size': '32px', 'color': 'red', }, []),
]),
]),
]);
let html=TAG.tagOfMethod(project);
document.open();document.write(html);
console.log(html);