HTML页面:
<div class="iphone">
</div>
<script type="text/javascript">
var cfg = {
type:'base',
text:'text',
}
var h5 = new H5ComponentBase('cfg');
$('.iphone').append(h5);
</script>
js页面:
var H5ComponentBase =function ( name,cfg ) {
var cfg = cfg || {};
var id = ( 'h5_c_'+Math.random() ).replace('.' , '_') ;
// 把当前的组件类型添加到样式中进行标记
var cls = 'h5_component_'+cfg.type;
var component = $('<div class="h5_component '+cls+' h5_component_name_'+name+'" id="'+id+'">');
cfg.text && component.text(cfg.text);
return component;
}
显示:
<div class="iphone">
<div class="h5_component h5_component_undefined h5_component_name_cfg" id="h5_c_0_9352051665679739"> </div>
</div>