File tree 1 file changed +29
-1
lines changed
1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ function renderDom(element) {
36
36
37
37
const {
38
38
type,
39
- props : { children } ,
39
+ props : { children, ... attributes } ,
40
40
} = element ;
41
41
42
42
if ( typeof type === 'string' ) {
@@ -69,9 +69,37 @@ function renderDom(element) {
69
69
}
70
70
}
71
71
72
+ updateAttributes ( dom , attributes ) ;
73
+
72
74
return dom ;
73
75
}
74
76
77
+ // 更新 dom 属性
78
+ function updateAttributes ( dom , attributes ) {
79
+ Object . keys ( attributes ) . forEach ( ( key ) => {
80
+ if ( key . startsWith ( 'on' ) ) {
81
+ // 事件的处理
82
+ const eventName = key . slice ( 2 ) . toLowerCase ( ) ;
83
+ dom . addEventListener ( eventName , attributes [ key ] ) ;
84
+ } else if ( key === 'className' ) {
85
+ // className 的处理
86
+ const classes = attributes [ key ] . split ( ' ' ) ;
87
+ classes . forEach ( ( classKey ) => {
88
+ dom . classList . add ( classKey ) ;
89
+ } ) ;
90
+ } else if ( key === 'style' ) {
91
+ // style处理
92
+ const style = attributes [ key ] ;
93
+ Object . keys ( style ) . forEach ( ( styleName ) => {
94
+ dom . style [ styleName ] = style [ styleName ] ;
95
+ } ) ;
96
+ } else {
97
+ // 其他属性的处理
98
+ dom [ key ] = attributes [ key ] ;
99
+ }
100
+ } ) ;
101
+ }
102
+
75
103
const ReactDOM = {
76
104
render,
77
105
} ;
You can’t perform that action at this time.
0 commit comments