index.js
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>DOM 节点操作</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="demo.js"></script> <link type="text/css" rel="stylesheet" href="style.css" /> </head> <body> <div>节点</div> <strong>DOM</strong> </body> </html>
?
demo.js
$(function(){ /* var box = $(‘<div id="box">节点</div>‘);//创建节点 $(‘body‘).append(box);//插入节点 //$(‘div‘).append(‘<strong>DOM</strong>‘); $(‘div‘).append(function(index,html){//节点的索引,节点的内容 return ‘<strong>DOM</strong>‘+index+html; }); $(‘strong‘).appendTo(‘div‘);//移入操作,不需要创建节点 //$(‘div‘).prepend(‘<strong>DOM</strong>‘); $(‘div‘).prepend(function(index,html){//节点的索引,节点的内容 return ‘<strong>DOM</strong>‘+index+html; }); $(‘strong‘).prependTo(‘div‘);//移入操作,不需要创建节点 $(‘div‘).after(function(index,html){ return ‘<strong>DOM</strong>‘+index+html; }); $(‘div‘).before(function(index,html){ return ‘<strong>DOM</strong>‘+index+html; }); */ //$(‘strong‘).insertAfter(‘div‘); $(‘strong‘).insertBefore(‘div‘); });
?
原文:http://onestopweb.iteye.com/blog/2224155