这算是对深表复制方法的一个很好的应用。

el : function(tagName, config){
	var _el = document.createElement(tagName)
	, defaultConfig = {
		innerHTML : 'I\'m a ' + tagName.toUpperCase() + '.',
		style : {
			width : '200px', height : '50px', backgroundColor : '#ccc'
		}, onclick : function(){
			this.innerHTML += '<hr />I\'m clicked.';
		}
	}
	muse.extend(defaultConfig, config);
	muse.extend(_el, defaultConfig);
	return _el;
}

var div = muse.el('div', {
	innerHTML : 'I\'m a DIV.',
	style : {
		width : '200px', height : '150px', backgroundColor : '#ff0', border : '1px solid #000'
	}, 
	onclick : function(){
		this.innerHTML += '<hr />I\'m clicked by a outer config.';;
	}
});
window.onload = function(){
	document.body.appendChild(div);
}
 

在配置文件中,写上dom的所有相关属性,extend会很好地把这些属性加载到dom上。

其实el在实际中要简单得多,这里是为了测试,才写了默认配置。实际应用的代码应该是:

el : function(tagName, config){
	var _el = document.createElement(tagName);
	muse.extend(_el, config);
	return _el;
}

相关文章:

  • 2021-10-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-16
  • 2021-06-05
  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
相关资源
相似解决方案