在jQ1.4以前我们创建DOM并且添加到DOM树大致是这样的:
var oNewp = $("<p>我测试成功了</p>");
oNewp.addClass("red").appendTo("#target");
或者,稍微复杂一点:
$("<div></div>")
.attr("id","css")
.height(90)
.css("border","1px solid #000")
.html("fuck world!")
.appendTo(document.body);
jQuery1.4带来了一个全新的便捷地清晰的DOM对象创建方法,在 jQuery 1.4中,我们可以传递一个对象作为第二个参数。 这个参数接受一个属性的集合,这些可以传递给.attr() 方法。此外,一些event type(事件类型)能通过, 而且后面的jQuery方法能够调用: val, css, html, text, data, width, height, or offset。详见:http://www.css88.com/jqapi/#p=jQuery
例如,我们创建一个文本框:
$("<input />",{
"class":"bg-yellow",
"id":"fuck",
css:{
"border":"1px solid #000",
"font-size":"25px"
},
value:"fuck world!",
focusin:function(){
$(this).css("border","5px solid #FF3300");
},
focusout:function(){
$(this).css("border","1px solid #000");
}
}).appendTo(document.body);
例如,我们创建一个容器:
$("<div></div>",{
"class":"bg-yellow",
"id":"fuckie",
css:{
"border":"1px solid #CDCDCD",
"font-size":"25px"
},
html:$("<a>",{
href: '#',
html:"hello world!",
click: function(event) {
$("#fuckie").css("background-color","#FF3300");
alert("fuck world!!!!!!!!!!");
event.preventDefault();
}
})
})
}).appendTo(document.body);
参看非常简陋的demo:http://www.css88.com/demo/jqueryCreatDom/
最新评论
可惜不能辞工。。。
Post:2012-05-14 22:24:43群都满了,求入群。 319031954.
Post:2012-05-14 16:41:39可惜不在广州
Post:2012-05-12 16:10:54厉害,受益匪浅!
Post:2012-05-12 13:48:02又在招人。可惜不是我。
Post:2012-05-12 11:50:12很好很好verygood
Post:2012-05-10 10:58:33json官方的定义是 A collection of »
Post:2012-05-09 20:23:49前端爱好者求入群啊 作品地址:http »
Post:2012-05-09 14:32:41