基于Jquery的动态创建DOM元素的代码
更新时间:2010年12月28日 13:22:32 作者:
jquery动态创建页面元素,mark一下,以备以后查询时使用。以创建div和input为例。
动态创建div:
$(function(){
$("<div>",{
id: 'test',
text: 'this is a test',
"class": "test",
click: function(){
$(this).toggleClass('test');
}
}).appendTo("body");
})
动态创建input:
$(function(){
$("<input>", {
type: 'text',
val: 'test',
focusin: function() {
$(this).addClass('active');
},
focusout: function() {
$(this).removeClass('active');
}
}).appendTo("body");
})
复制代码 代码如下:
$(function(){
$("<div>",{
id: 'test',
text: 'this is a test',
"class": "test",
click: function(){
$(this).toggleClass('test');
}
}).appendTo("body");
})
动态创建input:
复制代码 代码如下:
$(function(){
$("<input>", {
type: 'text',
val: 'test',
focusin: function() {
$(this).addClass('active');
},
focusout: function() {
$(this).removeClass('active');
}
}).appendTo("body");
})
相关文章
jQuery Ajax 实例代码 ($.ajax、$.post、$.get)
下面小编就为大家带来一篇jQuery Ajax 实例代码 ($.ajax、$.post、$.get)。小编觉得挺不错的,现在分享给大家,也给大家做个参考2016-04-04
最新评论