我想使用JavaScript替换页面中的整个HTML块:
<html>
<input type="number" id="totale">
<br>
<button onclick="primo();">esegui</button>
<ul>
<li id="figlio"></li>
<li id="figlia"></li>
<li id="genitori"></li>
</ul>
<html>
我想用段落元素(<ul>
)替换<p>
元素的整个块。我怎么能这样做?
答案 0 :(得分:0)
您可以使用此JavaScript来实现此目的:
// get the ul element you want to replace with
var ulTag = document.getElementsByTagName('ul')[0];
//create a p element
var paraObj=document.createElement("p");
//set the p text
paraObj.innerHTML='This is a new paragraph';
//get the parent element of the <ul>
var ObjParent=ulTag.parentNode;
//replace the <ul> with <p>
ObjParent.replaceChild(paraObj,ulTag);
<html>
<input type="number" id="totale"><br>
<button onclick="primo();">esegui</button>
<ul>
<li id="figlio"></li>
<li id="figlia"></li>
<li id="genitori"></li>
</ul>
<html>