Bởi vì bạn đang nói về sự thay thế của bạn là bất cứ điều gì, và cũng thay thế ở giữa trẻ em của một yếu tố, nó trở nên khó khăn hơn so với việc chỉ chèn một yếu tố đơn lẻ, hoặc trực tiếp loại bỏ và nối tiếp:
... Hoặc là hoặc tìm cách sử dụng jQuery và đó là phương thức
Vấn đề trên rõ ràng cũng phủ nhận giải pháp của tôi - tôi đã cập nhật mã của mình ở trên (ít nhất là đối với vấn đề
Nếu sau đó bạn đưa chuỗi này đưa nó vào DOM, tùy thuộc vào trình duyệt, bạn sẽ nhận được như sau:
Câu hỏi duy nhất còn lại là tại sao bạn muốn đạt được HTML bị hỏng ngay từ đầu? :)
Làm thế nào để thay thế Div bằng một Div khác trong JavaScript ?.
Bản sao đầu tiên div mong muốn ..
Thứ hai trống rỗng div chính ..
Thứ ba nối bản sao vào Div chính ..Xem thảo luậnLàm thế nào để thay thế Div bằng một Div khác trong JavaScript ?.
Bản sao đầu tiên div mong muốn ..
Thứ hai trống rỗng div chính ..
Thứ ba nối bản sao vào Div chính ..createElement(), createTextNode(), appendChild(), replaceChild() methods and childNodes property.
Chúng ta hãy thảo luận về các phương thức và thuộc tính này để thay thế một phần tử HTML sang một phần tử khác.
- createdEuity (): Nó được sử dụng để tạo một nút phần tử với tên được chỉ định.Syntax: ________ 5 It is used to create
an element node with the specified name.
Syntax:var element = document.createElement("Element_name");
Trong ví dụ này, phần tử là thẻ H2 H2, vì vậy hãy viết
var element=document.createElement("h2");
- createdetextNode (): Phương thức được sử dụng để tạo nút văn bản.syntax: ________ 7 The method is used to create a text node.
Syntax: var txt = document.createTextNode("Some_Text");
- appendChild (): Sau khi tạo nút văn bản, chúng ta phải nối nó vào phần tử bằng cách sử dụng phương thức appendChild (). After creating text node, we have to append it to the element by using appendChild() method.
Syntax:
element.appendChild(Node_To_append);
Sau đây cho thấy mã làm việc về cách sử dụng các phương thức và thuộc tính này được thảo luận ở trên.
HTML
replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
8replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
0jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
3jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
7jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9<table><tr>123 text<td>END</td></tr></table>
0jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5<table><tr>123 text<td>END</td></tr></table>
2jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
7jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9<table><tr>123 text<td>END</td></tr></table>
7jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
0jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
2123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
4jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
6jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
8jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5<table><tr>123 text<td>END</td></tr></table>
2<table><tr>123 text<td>END</td></tr></table>
7jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1<table><tr>123 text<td>END</td></tr></table>
2jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
3jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1<table><tr>123 text<td>END</td></tr></table>
2jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
0jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1Output:
Childnodes [Vị trí]: Thuộc tính này trả về một bộ sưu tập các nút con như một đối tượng Nodelist. Các nút được sắp xếp khi chúng xuất hiện trong mã nguồn và có thể được truy cập bằng số chỉ mục bắt đầu từ 0. This property returns a collection of child nodes as a NodeList object. The nodes are sorted as they appear in source code and can be accessed by index number starting from 0.
Thay thế (): Nó thay thế một nút con bằng nút mới. It replace a child node with new node.
old_Node.replaceChild(new_Node, old_node.childNodes[node_position]);
Ví dụ: Mã sau đây cho thấy cách thay thế phần tử bằng một mã khác. The following code shows how to replace element with another one.
HTML
replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
8replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
0jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
3jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9 var element=document.createElement("h2");
8 var element=document.createElement("h2");
9 var txt = document.createTextNode("Some_Text");
0 var txt = document.createTextNode("Some_Text");
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9 var txt = document.createTextNode("Some_Text");
5 var element=document.createElement("h2");
9 var txt = document.createTextNode("Some_Text");
0 var txt = document.createTextNode("Some_Text");
8 var txt = document.createTextNode("Some_Text");
9__755521jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9 element.appendChild(Node_To_append);
4 element.appendChild(Node_To_append);
5 var txt = document.createTextNode("Some_Text");
0 element.appendChild(Node_To_append);
7jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3old_Node.replaceChild(new_Node, old_node.childNodes[node_position]);
0jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9<table><tr>123 text<td>END</td></tr></table>
2 element.appendChild(Node_To_append);
4jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5<table><tr>123 text<td>END</td></tr></table>
2 var element=document.createElement("h2");
8jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1Các
jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
9<table><tr>123 text<td>END</td></tr></table>
7jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
13123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
15123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
17123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
19123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
21123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
23123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
25123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
27123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
29123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
31123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
33123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
35123 text<table><tr><td>END</td></tr></table>
<table><tr><td>END</td></tr></table>
3replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
37jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
9replaceTargetWith( 'idTABLE', 'I <b>can</b> be <div>anything</div>' );
39jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
5<table><tr>123 text<td>END</td></tr></table>
2<table><tr>123 text<td>END</td></tr></table>
7jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1<table><tr>123 text<td>END</td></tr></table>
2jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
3jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1<table><tr>123 text<td>END</td></tr></table>
2jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
0jQuery('#idTABLE').replaceWith('<blink>Why this tag??</blink>');
1Output:
Bạn có thể chỉnh sửa HTML-thẻ, thuộc tính và nội dung-trực tiếp trong khung HTML: Bấm đúp vào văn bản bạn muốn chỉnh sửa, thay đổi nó và nhấn Enter để xem các thay đổi được phản ánh ngay lập tức.Bạn có thể thêm bất kỳ HTML nào vào đây: Thay đổi thẻ của phần tử, thay đổi các phần tử hiện có hoặc thêm các phần tử mới.double-click the text you want to edit, change it, and press Enter to see the changes reflected immediately. You can add any HTML in here: changing the element's tag, changing existing elements, or adding new ones.