亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

python入門詳細(xì)教程 append函數(shù)是什么意思


python入門詳細(xì)教程 append函數(shù)是什么意思

文章插圖
append和appendChild是兩個(gè)常用的方法,用于將元素添加到文檔對(duì)象模型(DOM)中 。它們經(jīng)??梢曰Q使用,沒有太多麻煩,但如果它們是一樣的,那么為什么要出現(xiàn)兩個(gè)API呢?……它們只是相似,但不是一樣 。
append()此方法用于以Node對(duì)象或DOMString(基本上是文本)的形式添加元素 。
插入一個(gè)Node對(duì)象
const parent = document.createElement('div');const child = document.createElement('p');parent.append(child);// 這會(huì)將子元素追加到div元素// 然后div看起來像這樣<div> <p> </ p> </ div>這會(huì)將子元素追加到 div 元素,然后 div 看起來像這樣
<div> <p> </ p> </ div>插入DOMString
const parent = document.createElement('div');parent.append('附加文本');然后 div 看起來像這樣的
<div>附加文本</ div>appendChild()與 .append 方法類似,該方法用于DOM中的元素,但在這種情況下,只接受一個(gè)Node對(duì)象 。
插入一個(gè)Node對(duì)象
const parent = document.createElement('div');const child = document.createElement('p');parent.appendChild(child);這會(huì)將子元素追加到 div 元素,然后 div 看起來像這樣
<div> <p> </ p> </ div>插入DOMString
const parent = document.createElement('div');parent.appendChild('Appending Text');// Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'不同點(diǎn).append 接受Node對(duì)象和DOMString,而 .appendChild 只接受Node對(duì)象 。
const parent = document.createElement('div');const child = document.createElement('p');// 追加節(jié)點(diǎn)對(duì)象parent.append(child) // 工作正常parent.appendChild(child) // 工作正常// 追加DOMStringsparent.append('Hello world') // 工作正常parent.appendChild('Hello world') // 拋出錯(cuò)誤.append 沒有返回值,而 .appendChild 返回附加的Node對(duì)象 。
const parent = document.createElement('div');const child = document.createElement('p');const appendValue = https://www.520longzhigu.com/diannao/parent.append(child);console.log(appendValue) // undefinedconst appendChildValue = parent.appendChild(child);console.log(appendChildValue) //

.append 允許您添加多個(gè)項(xiàng)目,而 .appendChild 僅允許單個(gè)項(xiàng)目 。
const parent = document.createElement('div');const child = document.createElement('p');const childTwo = document.createElement('p');parent.append(child, childTwo, 'Hello world'); // 工作正常parent.appendChild(child, childTwo, 'Hello world');// 工作正常,但添加第一個(gè)元素,而忽略其余元素總結(jié)在可以使用 .appendChild 的情況下,可以使用 .append,但反過來不行 。
如果對(duì)你有所啟發(fā)和幫助,可以點(diǎn)個(gè)關(guān)注、收藏、轉(zhuǎn)發(fā),也可以留言討論,這是對(duì)作者的最大鼓勵(lì) 。
作者簡(jiǎn)介:Web前端工程師,全棧開發(fā)工程師、持續(xù)學(xué)習(xí)者 。


    以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請(qǐng)您及時(shí)就醫(yī)或請(qǐng)專業(yè)人士給予相關(guān)指導(dǎo)!

    「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對(duì)您有所幫助: