Post by thelisto on Feb 24, 2008 13:45:37 GMT -5
function each(a, f) { for(var i=0, l=a.length; i<l; i++) f(a) };
each('a big blockquote br b center code div em form h1 h2 h3 h4 h5 h6 hr img iframe input i li ol option pre p script select small span strong style sub sup table tbody td textarea tr ul u'.split(' '),
function(label){
window[label]=function(){
var tag=document.createElement(label);
each(arguments, function(arg){
if(arg.nodeType) tag.appendChild(arg);
else if(typeof arg=='string' || typeof arg=='number') tag.innerHTML+=arg;
else for(var attr in arg){
if(attr=='style') for(var sty in arg[attr]) tag[attr][sty]=arg[attr][sty];
else tag[attr]=arg[attr];
};
});
return tag;
};
});
div({style:{background:'lavender', padding:'10px', fontSize:'130%', textAlign:'center'}},
b('Hello '), span('world', {className:'pink'}), '!', br(), br(),
table({style:{margin:'0 auto', width:'300px'}},
tbody(
tr(
td({style:{background:'#ccc'}},
a({href:'http://javascript.crockford.com/'}, 'A link!')
),
td({style:{background:'#fff'}},
img({src:'/images/blog/logos/elzr.gif'})
)
)
)
)
)
Code Beyond this point <---- Isn't related
-
-
-
The translation between HTML and Hyperscript is straightforward, where you would have written
<b>Hello world!</b>,
you now write,
b(‘Hello World!’).
Instead of
<em style=”background-color:yellow”>Hello world!</em>,
now it’s,
em({style:{backgroundColor:’yellow’}},’Hello World!’).
And so on.
Sorry for this being so big.