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大鸟们帮帮手了。
谢谢
{
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大鸟们帮帮手了。
谢谢
解决方案 »
- Ext的treenode
- document.activeElement.parentElement.id
- MF与IE兼容性
- 从主窗口中打开一个模态对话框,如何在对话框中访问主窗口的函数?
- 如何写个正则表达式,将"c:\aaa\bbb\dd.jpg"替换成"c:/aaa/bbb/dd.jpg"
- 有关onclick 的问题,请教大家~
- 再次提问,关于取得form里<input>和<select>对象集合数量的方法~~~~
- 如何让同一个窗口载入一个新的页面,而不是新创建一个窗口来载入新页面
- javascript做的Excel
- ie 5 不支持这种javasript??
- 请教:如何获取appendChild()方法动态插入的元素的ID
- 急求:关于insertAdjacentHTML在非IE浏览器下的替代函数insertHtml(where, el, html)
<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>
拆成多个函数好?如果改成这样子好不好呢?
C('div').id('block').className('ico').title('图标');
也就等于:
var div = C('div');
div.id = 'block';
div.className = 'ico';
div.title = 'title';
你用prototype给c扩展了一个函数啊。。
如果弄成这样
C('div').id('block').className('ico').title('图标');那不是要prototype N个函数?
能不能捕捉未定义的然后执行类似这样:
eval('o.' + type + '= "' + msg +'";');
创建表格等....这段代码性能的关键在于msg.constructor==Array 中那段代码的处理?
那里只有个appendChild。应该挺快..
你如果弄成
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', '删除');
可能我没明白楼主的意思吧。想要怎么调用,楼主说详细点。
我小菜鸟一个。
确实是不大懂你那个用意。。
你给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('删除');
能实现么。
C('a').In('删除').className('ico2 ico_del2').title('删除');这个In可以是字符串。也可以是数组。
是字符串就innerHTML
是数组就appendChild
C(Object类型)
var obj = new Object(0;
obj.className="..."
obj.title="..."
obj......
你觉得呢
var obj = new Object();
obj.className="..."
obj.title="..."
obj......
调用: C(obj);
这个创建的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', '操作');
这样写很明显有些猥琐啊
这样就是有些疼蛋才弄了有时候
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');
是不是好些。呢。
<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>
不想为这个再加引用快上百k的js了。。
C('a', {'In':'删除', 'className':'ico2 ico_del2', 'title':'删除'})如果能这样最好了。C('a').In('删除').className('ico2 ico_del2').title('删除');
看样子有些麻烦 =_=?
<!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>