您好,欢迎访问一九零五行业门户网

如何使用 JavaScript 将 HTML 元素替换为另一个元素?

在本教程中,我们将学习使用 javascript 将 html 元素替换为另一个元素。
在某些用例中,我们需要用不同的元素替换整个 html 元素。例如,它在表单验证中非常重要。假设我们从表单中获取用户的年龄,并且用户输入了错误的年龄;我们可以通过替换 html 元素来显示验证消息。我们需要用另一个 html 元素替换 html 元素的另一个用例是动态内容加载,其中我们必须根据某些条件(例如位置等)显示网页内容。
在这里,我们将学习 3 种替换网页 html 元素的方法。
使用replacewith()方法第一种方法使用replacewith() 方法。我们需要通过将前一个元素作为引用并传递一个新元素作为引用来执行replacewith()方法。我们可以创建一个字符串格式的新元素。
语法用户可以按照以下语法使用replacewith() javascript方法将一个html元素替换为另一个html元素。
oldelement.replacewith(newelement);
在上面的语法中,oldelement是可替换元素,newelement是被替换元素。
示例在下面的示例中,我们在 html 中有一个包含“oldelement”id 的 div 元素。我们在单击按钮时调用 replaceelement() 函数。在replaceelement()函数中,我们使用createelement()方法创建一个新的“h2”元素。此外,我们使用 textcontent 属性将内容添加到新创建的 html 元素中。之后,我们使用replacewith()方法将oldelement替换为newelement。
在输出中,用户可以单击按钮并查看网页上的更改。
<html><body> <h3> using the <i> replacewith() method </i> to replace the html elements in javascript </h3> <div id = oldelement> this div is the old element. </div> <button onclick = replaceelement()> replace element </button> <script> function replaceelement() { var newelement = document.createelement('h2'); newelement.textcontent = 'this element is the new element.'; var oldelement = document.getelementbyid('oldelement'); oldelement.replacewith(newelement); } </script></html>
使用outerhtml属性任何元素的outerhtml属性都允许我们用另一个html元素替换整个html元素。我们需要为outerhtml属性分配一个新的元素值来替换html元素。
语法用户可以按照以下语法使用outerhtml属性将一个html元素替换为另一个html元素。
document.getelementbyid(id).outerhtml = newele;
示例在下面的示例中,我们创建了包含“para”id 和一些文本内容的“”元素。在replaceelement()函数中,我们以字符串格式创建一个新的html元素,并将其值赋给“”元素的outerhtml属性。
在输出中,用户应单击按钮将“”元素替换为“”html 元素。
<html><body> <h3> using the <i> outerhtml property </i> to replace the html elements in javascript </h3> <p id = para> welcome to the tutorialspoint! </p> <button onclick = replaceelement()> replace element </button> <script> function replaceelement() { let newele = '<h2> you clicked the button to replace the element. <h2>'; document.getelementbyid('para').outerhtml = newele; } </script></html>
示例在下面的示例中,我们创建了包含不同选项的选择菜单。在这里,我们将替换选择菜单的特定选项。此外,我们创建了两个输入字段。一种是获取索引号,另一种是获取用户的新选项值。
在replaceoption()函数中,我们从用户那里获取新的输入值。之后,我们使用用户在输入中输入的索引值来访问该选项。接下来,我们使用选项的outerhtml 属性将选项替换为新值。
在输出中,在第一个输入字段中输入从零开始的索引值,在第二个输入字段中输入新的选项值,然后单击按钮替换选项。
<html><body> <h3> using the <i> outerhtml property </i> to replace the options in the select menu </h3> <label for = indexinput> index: </label> <input type = number id = indexinput> <br> <br> <label for = valueinput> new value: </label> <input type = text id = valueinput> <br> <br> <select id = demo> <option value = 1> one </option> <option value = 2> two </option> <option value = 3 selected> three </option> <option value = 4> four </option> <option value = 5> five </option> <option value = 6> six </option> </select> <button onclick = replaceoption()> replace option </button> <script> function replaceoption() { // get user input var index = parseint(document.getelementbyid('indexinput').value); var newvalue = document.getelementbyid('valueinput').value; var list = document.getelementbyid('demo'); // get the option to replace var option = list.options[index]; // replace the option option.outerhtml = '<option value=' + newvalue + '>' + newvalue + '</option>'; } </script></html>
使用replacechild()方法replacechild() 方法允许开发人员用新元素替换特定 html 元素的子节点。在这里,我们可以替换特定元素的第一个子元素,以将其自身替换为新元素。
语法用户可以按照以下语法使用replacechild()方法将一个html元素替换为另一个html元素。
oldele.replacechild(htmlele, oldele.childnodes[0]);
在上面的语法中,我们需要将新元素作为replacechild()方法的第一个参数传递,将旧元素作为第二个参数传递。
示例在下面的示例中,首先我们使用 createelement() 方法创建了“h3”元素。之后,我们使用 createtextnode() 方法来创建文本内容。之后,我们使用appendchild()方法将文本节点附加到新创建的html元素中。
接下来,我们通过引用旧元素来执行replacechild()方法。此外,我们还传递了 htmlele 作为第一个参数(一个新创建的元素),以及 oldele.childnodes[0] 作为第二个参数(旧元素的第一个子元素,代表其自身)。
在输出中,单击按钮并观察网页上的变化。
<html><body> <h3> using the <i> replacechild() method </i> to replace the html elements in javascript </h3> <p id = para> welcome to the tutorialspoint! </p> <button onclick = replaceelement()> replace element </button> <script> function replaceelement() { // create a new element var htmlele = document.createelement(h3); // create a new text node var txt = document.createtextnode(this is a content of new element!); // use appendchild() to add a text node to the element htmlele.appendchild(txt); var oldele = document.getelementbyid(para); // using replacechild() to replace the old element with a new element oldele.replacechild(htmlele, oldele.childnodes[0]); } </script></html>
我们学习了三种使用 javascript 将一个 html 元素替换为另一个 html 元素的不同方法。在第一种方法中,我们使用了replacewith()方法,这是最好、最简单的方法之一。在第二种方法中,我们使用了outerhtml属性;在第三种方法中,我们使用了replacechild()方法,该方法一般用于替换子元素。
以上就是如何使用 javascript 将 html 元素替换为另一个元素?的详细内容。
其它类似信息

推荐信息