if(flag==0){
var p = document.createElement("p");
var text_p = document.createTextNode("還沒有賬號0?");
p.setAttribute("class","p-rj");
p.setAttribute("id","prj");
p.appendChild(text_p);
var a = document.createElement("a");
var text_a = document.createTextNode("注冊0");
a.setAttribute("href","javascript:void(0);");
a.setAttribute("class","drop_col");
a.setAttribute("id","register");
a.setAttribute("onclick","rjSecond()");
a.appendChild(text_a);
p.appendChild(a);
$("boss").appendChild(p); }//改了增加HTML內容之后。發現還是沒法獲得id,我就設置了屬性onclick最后bug解決
flag=1;
.....
/* $("register").onclick=function(){ 之前用的這個方式,但是沒辦法獲id的值register ,不知道為什么新增加的HTML里的,已經形成頁面,但是再次點擊這個綁定事件的標簽時,id沒法獲取到。*/
function rjSecond()
{ var table = document.getElementsByTagName("table")[0];
var form = table.parentNode;
form.removeChild(table)
if(flag!=0){
var div = $("submit-div");
form.removeChild(div);
........
=================
后來終於明白了如何成功獲取id了:要把所有代碼都放在那個大事件的代碼里:例如:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>shijian</title>
</head>
<body>
<div id="box">
<button id="myBtn">試一試</button>
</div>
<p id="demo"></p>
</body>
<script>
document.getElementById("box").innerHTML+="<p id='hh' >函數。</p>";
document.getElementById("demo").innerHTML=Date();
document.getElementById("hh").onclick=function () {
alert("gg");
};
};
</script>
</html>