var C = function (tag, type, msg)
{
var o = (typeof(tag) != 'object') ? document.createElement(tag) : tag;

if (type == 'In')
{
if(msg  && typeof(msg) == 'object') 
{
if(msg.length > 1 && msg.constructor == Array )
{
for (x in msg)
{
o.appendChild(msg[x]);
}
}
else
{
    o.appendChild(msg);
}
}
else
{
o.innerHTML = msg;
}
}
else if (typeof(type) == 'string')
{
    eval('o.' + type + '= "' + msg +'";');
}
return o;
}使用。C(C(C('a','In','删除'),'className','ico2 ico_del2'),'title','删除');C(this.Index_Data.table, 'In', [this.Index_Data.table_thead, this.Index_Data.table_tbody, this.Index_Data.table_tfoot]);等。。能优化下函数么。
要考虑到性能、使用方便。
因为到处都用这函数啊。这么搞不太方便。
也不知效率如何。csdn大鸟们帮帮手了。
谢谢

解决方案 »

  1.   

    适用Chaining模式,这样行不行
    <script type="text/javascript">
        function c(tag) {
            if (this instanceof c) {           
                this.o = (typeof(tag) != 'object') ? document.createElement(tag) : tag;
            }
            else {
                return new c(tag);
            }
        }
        c.prototype.fn = function (type, msg) {
            if (type == 'In') {
                if (msg && typeof (msg) == 'object') {
                    if (msg.length > 1 && msg.constructor == Array) {
                        for (x in msg) {
                            this.o.appendChild(msg[x]);
                        }
                    }
                    else {
                        this.o.appendChild(msg);
                    }
                }
                else {
                    this.o.innerHTML = msg;
                }
            }
            else if (typeof (type) == 'string') {            
                this.o[type] = msg;
            }
            
            return this;
        }    //使用:
        c('a').fn('In', '删除')
              .fn('className', 'ico2,ico_del2')
              .fn('title', '删除');
      
    </script>
      

  2.   

    木有人。是不是分太少? 都没鸟人啊。楼上的为何把eval去掉? 
    拆成多个函数好?如果改成这样子好不好呢?
    C('div').id('block').className('ico').title('图标');
    也就等于:
    var div = C('div');
    div.id = 'block';
    div.className = 'ico';
    div.title = 'title';
      

  3.   

    这个比我原先的清晰些了啊。
    你用prototype给c扩展了一个函数啊。。
    如果弄成这样
    C('div').id('block').className('ico').title('图标');那不是要prototype N个函数?
    能不能捕捉未定义的然后执行类似这样:
     eval('o.' + type + '= "' + msg +'";');
      

  4.   

    这段代码性能的关键在于msg.constructor==Array 中那段代码的处理 看lz的例子是创建表格操作吗?
      

  5.   

    我到处都用啊。。所有要创建元素的地方都用。
    创建表格等....这段代码性能的关键在于msg.constructor==Array 中那段代码的处理? 
    那里只有个appendChild。应该挺快..
      

  6.   

    楼主可能没看明白我的代码,也可能是我理解错了楼主的意思。
    你如果弄成
    C('div').id('block').className('ico').title('图标');
    那么你之前调用的三个C连在一起的
    C(C(C('a','In','删除'),'className','ico2 ico_del2'),'title','删除');
    这种你要怎么调用?
    ,我上面那种方法就简化成:
    c('a').fn('In', '删除')
          .fn('className', 'ico2,ico_del2')
          .fn('title', '删除');
    可能我没明白楼主的意思吧。想要怎么调用,楼主说详细点。
      

  7.   

    囧.. 
    我小菜鸟一个。
    确实是不大懂你那个用意。。
    你给c里面定个this.o
    然后给this.o操作等... 返回this。。
    你说说啊。我1楼那个三个C连在一起的。
    C(C(C('a','In','删除'),'className','ico2 ico_del2'),'title','删除');也就只是操作 最里面那个C创建的对象的啊。
    也就这个意思...
    C('a').In('block').className('ico2 ico_del2').title('删除');
    能实现么。
      

  8.   

    应该是
    C('a').In('删除').className('ico2 ico_del2').title('删除');这个In可以是字符串。也可以是数组。
    是字符串就innerHTML
    是数组就appendChild
      

  9.   

    我的建议是这样
    C(Object类型)
    var obj = new Object(0;
    obj.className="..."
    obj.title="..."
    obj......
    你觉得呢
      

  10.   


    var obj = new Object();
    obj.className="..."
    obj.title="..."
    obj......
    调用: C(obj);
      

  11.   

    C('a').In('删除').className('ico2 ico_del2').title('删除');
    这个创建的HTML就是:
    <a class="ico2 ico_del2" title="删除">删除</a>如果用1楼函数就得这么写:
    C(C(C('a', 'In', '删除'),'className', 'ico2 ico_del2'), 'title', '删除');----------------------------------------------------------------------------C('div').In([C('a').In('编辑'), C('a').In('删除')]).id('block').title('操作');
    这个创建的HTML就是
    <div id="block" title="操作"><a>编辑</a><a>删除</a></div>如果用1楼函数就得这么写:
    C(C(C('div', 'In', [C('a', 'In', '编辑'), C('a', 'In', '删除')]), 'id' , 'block'), 'title', '操作');
    这样写很明显有些猥琐啊
      

  12.   


    这样就是有些疼蛋才弄了有时候
    this.ActionTr.ShowIndex.button.id = 'XXX'
    this.ActionTr.ShowIndex.button.className = 'XXX'
    this.ActionTr.ShowIndex.button.innerHTML = 'XXX'
    ...一大堆。
    如果能这样
    this.ActionTr.ShowIndex.button.id('xxx').className('xxx').innerHTML('xxx');
    是不是好些。呢。
      

  13.   

    这种的话,用jquery就可以啦。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script src="http://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $('<a>', {
                    'text': '删除',
                    'title': '删除',
                    'class': 'ico2 ico_del2'
                }).appendTo('body');            var div = $('<div>', {
                    'id': 'block',
                    'title': '操作'
                }).appendTo('body');
                $('<a>', {
                    'title': '编辑'
                }).appendTo(div);
                $('<a>', {
                    'title': '编辑'
                }).appendTo(div);            alert($('body').html());
            });
            
        </script></head>
    <body></body>
    </html>
      

  14.   

    大叔。没用jq呢。
    不想为这个再加引用快上百k的js了。。
      

  15.   

    怎么没想到用{}早知道这么干好了。
    C('a', {'In':'删除', 'className':'ico2 ico_del2', 'title':'删除'})如果能这样最好了。C('a').In('删除').className('ico2 ico_del2').title('删除');
    看样子有些麻烦 =_=?
      

  16.   

    那就这样嘛
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
    <div id="content"></div><script type="text/javascript">    function $(tag) {
            if (this instanceof $) {           
            this.o = document.createElement(tag);
            }
            else {
                return new $(tag);
            }
        }      $.prototype = {
            fn: function (attributes) {
                this.setProperties(this.o, attributes);
                return this;
            },
            get: function (node) {// 获取对象
                return typeof (node) == 'string' ? document.getElementById(node) : node;
            },       
            setProperties: function (element, properties) { //设置属性
                var val;
                for (var key in properties) {
                    val = properties[key];
                    if (key == 'style') {
                        element.style.cssText = val;
                    } else if (key == 'class') {
                        element.className = val;
                    } else if (key == 'for') {
                        element.htmlFor = val;
                    } else if (key == 'text') {
                        element.innerHTML = val;
                    } else {
                        element[key] = val;
                    }
                }
                return element;
            },
            appendTo: function (parentNode) {// 创建DOM对象             
                this.get(parentNode).appendChild(this.o);
                return this;
            }
        }    //调用
        $('a').fn({
            'class': 'ico2 ico_del2',
            'title':'删除',
            'text':'删除'
        }).appendTo('content');    var parentNode = $('div').fn({
            'id':'block',
            'title': '操作'
        }).appendTo('content');    $('a').fn({
            'text': '编辑'
        }).appendTo(parentNode.o);
        $('a').fn({
            'text': '删除'
        }).appendTo(parentNode.o);
        
                  
        alert(document.getElementById('content').innerHTML);
    </script></body>
    </html>
     
      

  17.   

    能实现这样么?C('a').In('删除').className('ico2 ico_del2').title('删除');