JavaScript HTML DOM 元素 (節(jié)點(diǎn))


Create Element

創(chuàng)建一個(gè)新的HTML DOM 節(jié)點(diǎn)元素


To add a new element to the HTML DOM, you must create the element (element node) first, and then append it to an existing element.

為了添加一個(gè)新的element,必須提前創(chuàng)建一個(gè)節(jié)點(diǎn),然后把它綁定到已經(jīng)存在的一個(gè)元素節(jié)點(diǎn)上;

例子:

This is a paragraph.

This is another paragraph.

var para = document.createElement("p");

var node = document.createTextNode("This is new.");

para.appendChild(node);

var element = document.getElementById("div1");

element.appendChild(para);


例子解讀:

This code creates a new

element:

var para = document.createElement("p");

To add text to the element, you must create a text node first. This code creates a text node:

var node = document.createTextNode("This is a new paragraph.");

Then you must append the text node to the element:

para.appendChild(node);

Finally you must append the new element to an existing element.This code finds an existing element:

var element = document.getElementById("div1");

This code appends the new element to the existing element:

element.appendChild(para);


Creating new HTML Elements - insertBefore()

The appendChild() method in the previous example, appended the new element as the last child of the parent.

If you don't want that you can use the insertBefore() method:

Example

This is a paragraph.

This is another paragraph.

var para = document.createElement("p");

var node = document.createTextNode("This is new.");

para.appendChild(node);

var element = document.getElementById("div1");

var child = document.getElementById("p1");

element.insertBefore(para,child);

Try it Yourself ?

Removing Existing HTML Elements

To remove an HTML element, you must know the parent of the element:

Example

This is a paragraph.

This is another paragraph.

var parent = document.getElementById("div1");

var child = document.getElementById("p1");

parent.removeChild(child);

Try it Yourself ?

The method node.remove() is implemented in the DOM 4 specification.

But because of poor browser support, you should not use it.

Example Explained

This HTML document contains a

element with two child nodes (two

elements):

This is a paragraph.

This is another paragraph.

Find the element with id="div1":

var parent = document.getElementById("div1");

Find theelement with id="p1":

var child = document.getElementById("p1");

Remove the child from the parent:

parent.removeChild(child);

It would be nice to be able to remove an element without referring to the parent.

But sorry. The DOM needs to know both the element you want to remove, and its parent.

Here is a common workaround: Find the child you want to remove, and use its parentNode property to find the parent:

var child = document.getElementById("p1");

child.parentNode.removeChild(child);


Replacing HTML Elements (替換元素)


To replace an element to the HTML DOM, use the replaceChild() method:

Example

This is a paragraph.

This is another paragraph.

var para = document.createElement("p");

var node = document.createTextNode("This is new.");

para.appendChild(node);

var parent = document.getElementById("div1");

var child = document.getElementById("p1");

parent.replaceChild(para,child);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末痊乾,一起剝皮案震驚了整個(gè)濱河市络断,隨后出現(xiàn)的幾起案子射亏,更是在濱河造成了極大的恐慌铁瞒,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件桐筏,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡蘸泻,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門嘲玫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)悦施,“玉大人,你說(shuō)我怎么就攤上這事去团÷盏” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵土陪,是天一觀的道長(zhǎng)昼汗。 經(jīng)常有香客問(wèn)我,道長(zhǎng)鬼雀,這世上最難降的妖魔是什么顷窒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮源哩,結(jié)果婚禮上鞋吉,老公的妹妹穿的比我還像新娘。我一直安慰自己励烦,他們只是感情好谓着,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著坛掠,像睡著了一般赊锚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上屉栓,一...
    開(kāi)封第一講書(shū)人閱讀 52,736評(píng)論 1 312
  • 那天舷蒲,我揣著相機(jī)與錄音,去河邊找鬼系瓢。 笑死阿纤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的夷陋。 我是一名探鬼主播,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼胰锌,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼骗绕!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起资昧,我...
    開(kāi)封第一講書(shū)人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤酬土,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后格带,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體撤缴,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡刹枉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屈呕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片微宝。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖虎眨,靈堂內(nèi)的尸體忽然破棺而出蟋软,到底是詐尸還是另有隱情,我是刑警寧澤嗽桩,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布岳守,位于F島的核電站,受9級(jí)特大地震影響碌冶,放射性物質(zhì)發(fā)生泄漏湿痢。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一扑庞、第九天 我趴在偏房一處隱蔽的房頂上張望譬重。 院中可真熱鬧,春花似錦嫩挤、人聲如沸害幅。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)以现。三九已至,卻和暖如春约啊,著一層夾襖步出監(jiān)牢的瞬間邑遏,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工恰矩, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留记盒,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓外傅,卻偏偏與公主長(zhǎng)得像纪吮,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子萎胰,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,586評(píng)論 0 23
  • 楊春布從比村里所有的小女孩都漂亮碾盟,錐子臉,大眼睛技竟,長(zhǎng)睫毛冰肴。可她的父親并不喜歡她,她的母親很早就去世了熙尉,父親是個(gè)...
    危險(xiǎn)份子閱讀 234評(píng)論 0 0
  • 這兩天廈門出奇的熱联逻。我卻出奇的想念北國(guó)秋日暖陽(yáng)下的那一片金黃。 閑暇之余检痰,汗流浹背地刷朋友圈包归,家鄉(xiāng)的親...
    潤(rùn)珠閱讀 226評(píng)論 0 0
  • 橙色小蘑菇閱讀 85評(píng)論 0 9
  • 出生于農(nóng)村箫踩,小時(shí)候天很藍(lán),我往返于農(nóng)田與家園谭贪。記得境钟,那時(shí)候坐著父親的三輪車,經(jīng)常期盼著開(kāi)往大縣城俭识,然而慨削,并沒(méi)有。整...
    哈默老師閱讀 1,221評(píng)論 5 26