① js如何添加刪除div
document.createElement()是在對象中創建一個對象,要與appendChild() 或 insertBefore()方法聯合使用。
其中,appendChild() 方法在節點的子節點列表末添加新的子節點。insertBefore() 方法在節點的子節點列表任意位置插入新的節點。
1、添加DIV
function addDiv(w,h){
//如果原來有「divCell」這個圖層,先刪除這個圖層
deleteDiv();
//創建一個div
var newdiv = document.createElement("divCell");
//添加到頁面
document.body.appendChild(newdiv);
//通過樣式指定該div的位置方式,若是想要自己設置div的位置,這句話必須有,把它注釋掉你就可以知道效果拉~試試看
newdiv.style.position="absolute";
//通過樣式指定x坐標(隨機數0~450)
newdiv.style.top= Math.round(Math.random()*450);
//通過樣式指定y坐標(隨機數0~700)
newdiv.style.left= Math.round(Math.random()*700);
//通過樣式指定寬度
newdiv.style.width=w;
//通過樣式指定高度
newdiv.style.height=h;
//通過樣式指定背景顏色,,若是背景圖片 例為 newdiv.style.backgroundImage="url(img/3.jpg)"
newdiv.style.backgroundColor="#ffffcc";
//添加div的內容
//newdiv.innerHTML=i++;
//設置樣式透明
newdiv.style.filter = "alpha(opacity=50)";
//設置ID
newdiv.id = "divCell";
}
2、刪除DIV
function deleteDiv()
{
var my = document.getElementById("divCell");
if (my != null)
my.parentNode.removeChild(my);
}
② js如何動態刪除指定id系列元素
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="utf-8">
</head>
<body>
<divid="a">
<divid="b"></div>
<divid="c1"></div>
<divid="c2"></div>
</div>
</body>
<script>
document.querySelectorAll("#adiv").forEach(item=>{
if(item.getAttribute("id").indexOf("c")!=-1){
item.remove()
}
})
</script>
</html>
請採納
③ 關於js動態增加刪除textarea的問題,增加可以,刪除不行。用input控制可以,用herf控制不行!
A標簽沒有form屬性,所以this.form會失敗,但INPUT標簽有form屬性,可以返回所在表單.
只需要改一下調用即可,
<a href="#" onclick="addCheckDetail(document.getElementById('myForm'),this.parentNode)" >
另一處this.form按同樣的方法修改.
④ js已動態添加的行當外部選項改變再添加新行或刪除
上代碼
新鮮出爐
<!DOCTYPEHTML>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>NewWebProject</title>
<scriptclass="jquerylibrary"src="http://runjs.cn/js/sandbox/jquery/jquery-1.8.2.min.js"type="text/javascript"></script>
<scripttype="text/javascript">
functionadd()
{
varaddMode=$("input[name='mode']:checked").val();
if(!addMode){
returnfalse;
}
varrows=$("#muliteRowtr[class!='newRow']");
if(addMode==="b"){
rows.before("<trclass='newRow'><td>新行</td></tr>");
}elseif(addMode==="a"||addMode==="c"){
rows.each(function(){
$(this).prev(".newRow").remove();
});
}
}
</script>
</head>
<body>
<h1>NewWebProjectPage</h1>
<tableid="muliteRow">
<tr><td>行一</td></tr>
<tr><td>行二</td></tr>
<tr><td>行三</td></tr>
<tr><td>行四</td></tr>
</table>
<td>
<inputtype="radio"name="mode"value="a"/>a
<inputtype="radio"name="mode"value="b"/>b
<inputtype="radio"name="mode"value="c"/>c
</td>
<inputtype="button"value="添加"onclick="add()"/>
</body>
</html>
⑤ js 怎麼刪除某個div裡面table除了最後一個td 其它都刪除
<!>
<html>
<head>
<metacharset="UTF-8"/>
<title>last.html</title>
<style>
div{
border:5pxsolidblack;
}
</style>
<scripttype="text/javascript">
onload=function()
{
vartable=div.getElementsByTagName('table')[0];
vartbody=table.tBodies[0];
varrow=tbody.rows[0],cells=row.cells;
while(cells.length!=1)
{
row.deleteCell(0);
}
}
</script>
</head>
<body>
<divid="div"class="test">
<tablewidth="200"border="0">
<tr>
<td>xx</td>
<td>xx</td>
<td>xxx</td>
<td>4</td>
</tr>
</table>
<div>1111</div>
</div>
</body>
</html>
⑥ JS 如何刪除當前節點內容
需要准備復的材料分別是:制電腦、html編輯器、瀏覽器。
1、首先,打開html編輯器,新建一個html文件,例如:index.html。