13

動態(tài)創(chuàng)建表格

var json='[{"ename":"Tom", "salary":10000, "age":25},{"ename":"John", "salary":11000, "age":28},{"ename":"Mary", "salary":12000, "age":25}]';

? ? ? ? ? ? ? ? var emps=eval(json);


? ? ? ? ? ? ? ? var table=document.createElement('table');

? ? ? ? ? ? ? ? var thead=document.createElement('thead');

? ? ? ? ? ? ? ? var tr=document.createElement('tr');

? ? ? ? ? ? ? ? for(var key in emps[0]){

? ? ? ? ? ? ? ? var th=document.createElement('th');

? ? ? ? ? ? ? ? th.innerHTML=key;

? ? ? ? ? ? ? ? tr.appendChild(th);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? thead.appendChild(tr);

? ? ? ? ? ? ? ? table.appendChild(thead);

? ? ? ? ? ? ? ? //創(chuàng)建tbody

? ? ? ? ? ? ? ? var tbody=document.createElement('tbody');

? ? ? ? ? ? ? ? for(var i=0;i

? ? ? ? ? ? ? ? ? ? //創(chuàng)建行

? ? ? ? ? ? ? ? ? ? var tr=document.createElement('tr');

? ? ? ? ? ? ? ? ? ? tbody.appendChild(tr);

? ? ? ? ? ? ? ? ? ? //創(chuàng)建td

? ? ? ? ? ? ? ? ? ? for(var key in emps[i]){

? ? ? ? ? ? ? ? ? ? ? ? var td=document.createElement('td');

? ? ? ? ? ? ? ? ? ? ? ? td.innerHTML=emps[i][key];

? ? ? ? ? ? ? ? ? ? ? ? tr.appendChild(td);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? table.appendChild(tbody);

? ? ? ? ? ? ? ? document.getElementById('data').appendChild(table);

仿微博

var inputVal=document.querySelector('.content>input');

? ? ? ? ? ? console.log(inputVal);

? ? ? ? ? ? var qwe=/^\s+$/;

? ? ? ? ? ? //獲取button

? ? ? ? ? ? var btn=document.querySelector('.content>button');

? ? ? ? ? ? btn.onclick=function(){

? ? ? ? ? ? ? ? if(inputVal.value==''||qwe.test(inputVal.value)){


? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? //動態(tài)創(chuàng)建元素:

? ? ? ? ? ? ? ? var div=document.createElement('div');

? ? ? ? ? ? ? ? div.className='main';

? ? ? ? ? ? ? ? var img=document.createElement('img');

? ? ? ? ? ? ? ? img.src='img/1.jpg';

? ? ? ? ? ? ? ? div.appendChild(img);

? ? ? ? ? ? ? ? var p=document.createElement('p');

? ? ? ? ? ? ? ? p.innerHTML=inputVal.value;

? ? ? ? ? ? ? ? div.appendChild(p);

? ? ? ? ? ? ? ? var button=document.createElement('button');

? ? ? ? ? ? ? ? button.innerHTML='刪除';

? ? ? ? ? ? ? ? div.appendChild(button);

? ? ? ? ? ? ? ? document.querySelector('.container').appendChild(div);

? ? ? ? ? ? ? ? ? ? button.onclick=function(){

? ? ? ? ? ? ? ? ? ? this.parentElement.parentElement.removeChild(this.parentElement);

? ? ? ? ? ? ? ? }


? ? ? ? ? ? }

? ? ? ? ? ? }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容