0
我想創建一個外部JavaScript文件中的一個函數,我可以稍後重用(即模塊化函數足夠通用,可用於任何需要的JavaScript)。我想用純JavaScript來做到這一點,而不是使用jQuery或innerHTML。javascript removeChild然後用新文本替換這個使用insertBefore
我想這種模塊化功能做到以下幾點: 1)刪除子 2)的元素與新的子值替換這個孩子
例如說你有這個代碼:
<p id="removeMe">This text needs to be removed</p>
<p id="nextPara">This is the paragraph that follows the replaced paragraph</p>
所以我需要:
1) parentNode.removeChild(removeMe);
2) var replacementParagraph = document.createElement('p');
3) var replacementParagraphText =document.createTextNode("This is the replacementParagraph text);
4) replacementParagraph.appendChild(replacementParagraphText);
現在我只需要弄清楚如何寫模塊化的功能有點像貝爾流量:
function removeReplace (parameter, parameter) {
1) remove the child of the paragraph with the id of "removeMe"
2) use insertBefore(newText, nextPara) to replace the child that was removed in step 1
}
非常感謝你的幫助,傑森
謝謝Bergi。這似乎是我需要的,但我不能得到它的工作 – HoppedUpDesigns
[Worksforme](http://jsfiddle.net/HfzPd/)? – Bergi