用js操作table、tr、td 「字体样式及TD背景图片」
function messageSort() {??--函數名
var message=document.getElementById("message").value;?--添加的內容(下面有對應的html)
if(name == "" ) return; --如果添加為空,返回
var row = document.createElement("tr"); //創建tr的
row.setAttribute("id", name); --設置row的屬性.?
var cell = document.createElement("td");//創建td
cell.appendChild(document.createTextNode(name));//td里注入文本
row.appendChild(cell);//將TD注入TR
var deleteButton = document.createElement("input"); //這部分是添加刪除button按鈕
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "刪除");
deleteButton.onclick = function () { deleteSort(name); };
cell = document.createElement("td");?
cell.appendChild(deleteButton);//注入按鈕
row.appendChild(cell);//將TD注入TR
document.getElementById("sortList").appendChild(row);//將TR注入到相應地方(sortList可以看下面html)
var cell5 = document.createElement("td");
cell.style.background="#ffffff";//背景顏色設置
row1.style.color="#ffffff"; //字體顏色設置
cell5.style.display = "none" ;?? //ie不支持setAttribute("style",?"display:none");
// <td? style="display:none"? >dd</td> 直接寫TD是這樣..
cell5.appendChild(document.createTextNode(zdid));
row.appendChild(cell5);
}
// 刪除內容
function deleteSort(id) {//這個函數為上面的刪除button調用的
var rowToDelete = document.getElementById(id);
var sortList = document.getElementById("sortList");
sortList.removeChild(rowToDelete);
}
</script>
</head>
<body>
<table border="0" cellspacing="0" width="400" bgcolor="#f5efe7">
<tr>
??? <td height="20">增加內容:</td>
??? <td><input id="message" type="text"></td>
??? <td><a href="javascript:messageSort();">添加</a></td>
</tr>
</table>
<table border="1" width="400">
<tr>
??? <td height="20" align="center">內容:</td>
??? <td>操作</td>
</tr>
<tbody id="sortList"></tbody>
</table>
</body>
?
ps:? document.style.backgroundImage="url('http://t1.baidu.com/it/u=1300390954,3399399465&fm=0&gp=0.jpg')"; //改變背景圖片??
?$("#demo_table").attr("background","2.jpg");//jquery
轉載自:http://blog.csdn.net/liujs_vb/article/details/6939473
轉載于:https://www.cnblogs.com/fyq891014/archive/2012/07/12/2990423.html
總結
以上是生活随笔為你收集整理的用js操作table、tr、td 「字体样式及TD背景图片」的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 乐分卡和信用卡的区别
- 下一篇: 通过命令行使用 JAX-WS调用webs