jQuery1.4中创建DOM的新方法
时间:2011年01月06日作者:愚人码头查看次数:4,249 views评论次数:5
在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-02-08 21:59:12@linda, 没见过世面吧你,老板才8 »
Post:2012-02-07 18:11:22码头哥,换个链接,http://www.ijo »
Post:2012-02-07 17:18:45currying: 科里化
Post:2012-02-06 15:51:18貌似在线api已经下不了了
Post:2012-02-06 11:25:19图片被防盗链了,哈哈~
Post:2012-02-06 10:29:45对你造成的不便非常抱歉,简易你卸 »
Post:2012-02-05 14:45:16您好,请问air版本的自动更新怎么去 »
Post:2012-02-05 12:06:24