本文實例講述了JavaScript實現動態添加、移除元素或屬性的方法。愛掏網 - it200.com分享給大家供大家參考,具體如下:
JavaScript 動態添加、移除元素
appendChild(newnode)
向節點的子節點列表的末尾添加新的子節點。愛掏網 - it200.com
insertBefore(newnode, existingnode)
在已有子節點之前插入新的子節點。愛掏網 - it200.com
removeChild(node)
刪除元素的某個指定的子節點,并以 Node 對象返回被刪除的節點,如果節點不存在則返回 null。愛掏網 - it200.com
innerHTML
屬性設置或返回表格行的開始和結束標簽之間的 HTML。愛掏網 - it200.com
測試用例
Middle ChildHello World
JavaScript 動態添加、移除屬性
setAttribute(attributename, attributevalue)
添加指定的屬性,并為其賦指定的值。愛掏網 - it200.com將屬性設置為undefined等同于刪除。愛掏網 - it200.com
removeAttribute(attributename)
刪除指定的屬性。愛掏網 - it200.com
getAttributeNode(attributename)
以 Attr 對象返回指定屬性名的屬性值。愛掏網 - it200.com
removeAttributeNode(attributenode)
刪除 Attr 形式指定的屬性,同時返回被刪除的Attr 形式的屬性。愛掏網 - it200.com
測試用例
0 Hello World
1 Hello World : font-size、font-style等,這些屬性脫離style單獨設置是無效的
2 Hello World setAttribute
3 Hello World setAttribute
4 Hello World removeAttribute
5 Hello World getAttributeNode & removeAttributeNode
為了學習工作與休閑娛樂互不沖突,現新建圈【碼農茶水鋪】用于程序員生活,愛好,交友,求職招聘,吐槽等話題交流,希望各位大神工作之余到茶水鋪來喝茶聊天。愛掏網 - it200.com群號:582735936