使用Javascript将HTML中的段落替换为新段落

使用Javascript将HTML中的段落替换为新段落,javascript,function,onload,getelementbyid,appendchild,Javascript,Function,Onload,Getelementbyid,Appendchild,我试图用Javascript创建的段落替换HTML文档中的p。页面加载后,two将替换为t var two = document.getElementById("two"); document.onload = function myFunction() { var p = document.createElement("p"); var t = document.createTextNode("I am the superior text"); p.appendChild(t);

我试图用Javascript创建的段落替换HTML文档中的
p
。页面加载后,
two
将替换为
t

var two = document.getElementById("two");

document.onload = function myFunction() {
  var p = document.createElement("p");
  var t = document.createTextNode("I am the superior text");
  p.appendChild(t);
  document.getElementById("p");
  document.two = p;
};

您只需替换
#two
元素的文本内容即可:

var two=document.getElementById(“two”);
window.onload=函数(){
var t=“我是上级文本”;
2.textContent=t;
};

Lorem ipsum dolor sit amet.

您只需替换
#two
元素的文本内容即可:

var two=document.getElementById(“two”);
window.onload=函数(){
var t=“我是上级文本”;
2.textContent=t;
};

Lorem ipsum dolor sit amet.

您只需替换
#two
元素的文本内容即可:

var two=document.getElementById(“two”);
window.onload=函数(){
var t=“我是上级文本”;
2.textContent=t;
};

Lorem ipsum dolor sit amet.

您只需替换
#two
元素的文本内容即可:

var two=document.getElementById(“two”);
window.onload=函数(){
var t=“我是上级文本”;
2.textContent=t;
};

Lorem ipsum dolor sit amet.

您不能直接将节点替换到文档中,您可以尝试使用innerHTML:

document.onload = function () {
  document.getElementById("two").innerHTML = "I am the superior text";
};

您不能直接将节点替换到文档中,您可以尝试使用innerHTML:

document.onload = function () {
  document.getElementById("two").innerHTML = "I am the superior text";
};

您不能直接将节点替换到文档中,您可以尝试使用innerHTML:

document.onload = function () {
  document.getElementById("two").innerHTML = "I am the superior text";
};

您不能直接将节点替换到文档中,您可以尝试使用innerHTML:

document.onload = function () {
  document.getElementById("two").innerHTML = "I am the superior text";
};

您不使用jquery有什么原因吗?为什么不替换段落
p
的内容呢?不使用jquery有什么原因吗?为什么不替换段落
p
的内容呢?不使用jquery有什么原因吗?为什么不替换段落
p
的内容呢?不使用jquery有什么原因吗?你为什么不直接替换段落
p
的内容呢?