js怎样添加、移除、移动、复制、创建和查找节点?

JavaScript011

js怎样添加、移除、移动、复制、创建和查找节点?,第1张

添加节点append;移除节点removeChild;移动节点:var sdds=document.getElementById;创建节点createTextNode();查找节点:document.getElementsByTagName。

其他方法:

创建新节点

createDocumentFragment() //创建一个DOM片段

createElement() //创建一个具体的元素

createTextNode() //创建一个文本节点

添加、移除、替换、插入

appendChild() //添加

removeChild() //移除

replaceChild() //替换

insertBefore() //插入

查找

getElementsByTagName() //通过标签名

getElementsByName() //通过元素的Name属性的值

getElementById() //通过元素Id,唯一性

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制。

document.createElement()是在对象中创建一个对象,要与appendChild() 或 insertBefore()方法联合使用。\x0d\x0a其中,appendChild() 方法在节点的子节点列表末添加新的子节点。insertBefore() 方法在节点的子节点列表任意位置插入新的节点。\x0d\x0a1、添加DIV \x0d\x0afunction addDiv(w,h){ \x0d\x0a//如果原来有“divCell”这个图层,先删除这个图层\x0d\x0adeleteDiv()\x0d\x0a//创建一个div \x0d\x0avar newdiv = document.createElement("divCell") \x0d\x0a//添加到页面 \x0d\x0adocument.body.appendChild(newdiv) \x0d\x0a//通过样式指定该div的位置方式,若是想要自己设置div的位置,这句话必须有,把它注释掉你就可以知道效果拉~试试看 \x0d\x0anewdiv.style.position="absolute" \x0d\x0a//通过样式指定x坐标(随机数0~450) \x0d\x0anewdiv.style.top= Math.round(Math.random()*450) \x0d\x0a//通过样式指定y坐标(随机数0~700) \x0d\x0anewdiv.style.left= Math.round(Math.random()*700) \x0d\x0a//通过样式指定宽度 \x0d\x0anewdiv.style.width=w \x0d\x0a//通过样式指定高度 \x0d\x0anewdiv.style.height=h \x0d\x0a//通过样式指定背景颜色,,若是背景图片 例为 newdiv.style.backgroundImage="url(img/3.jpg)" \x0d\x0anewdiv.style.backgroundColor="#ffffcc" \x0d\x0a//添加div的内容 \x0d\x0a//newdiv.innerHTML=i++\x0d\x0a//设置样式透明\x0d\x0anewdiv.style.filter = "alpha(opacity=50)"\x0d\x0a//设置ID\x0d\x0anewdiv.id = "divCell" \x0d\x0a }\x0d\x0a2、删除DIV \x0d\x0a function deleteDiv()\x0d\x0a {\x0d\x0avar my = document.getElementById("divCell")\x0d\x0aif (my != null)\x0d\x0amy.parentNode.removeChild(my)\x0d\x0a }

<!DOCTYPE html>

<html lang="">

<head>

<meta charset="gb2312">

<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, user-scalable=no">

<title>123</title>

<script src="Scripts/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function(){

var spotMax = 30

if($('div.spot').size() >= spotMax) {$(obj).hide()}

$("input#add").click(function(){     addSpot(this, spotMax)

})

})

function addSpot(obj, sm) {

$('div#spots').append(

'<div>' +

'<span><input name="shengfen" type="text" /></span>'+

'<span><input name="diqu" type="text" /></span>'+

'<input type="button" class="remove spot01" value="删除行" /></div>')

.find("input.remove").click(function(){

$(this).parent().remove()

$('input#add').show()

})

if($('div.spot').size() >= sm) {$(obj).hide()}

}

</script>

</head>

<body>

<span><input name="shengfen" type="text" /></span>

<span><input name="diqu" type="text" /></span>

<input type="submit" id="add" name="Submit" value="添加行" >

<div>

<form method="post" name="asdf" id="asdf"><div id="spots"></div></form>

</div>

</body>

</html>

效果这样(样式再自己调)

要加jQuery插件进去

不懂再追问