Hướng dẫn replace outerhtml javascript - thay thế externalhtml javascript

1

Show

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Có một nút dom (tức là:

var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
1) và một chuỗi thô chứa nó là sự thay thế mới.

Một kỹ thuật tốt để đạt được một cách hiệu quả ...

var rawReplacement = `

hello world!!!

` document.getElementById("myNode").outerHTML = rawReplacement

Nhưng không thực sự sử dụng outerhtml (tách phần tử ban đầu khỏi các sự kiện mất, v.v.).

Hỏi ngày 13 tháng 9 năm 2016 lúc 15:43Sep 13, 2016 at 15:43

Hướng dẫn replace outerhtml javascript - thay thế externalhtml javascript

6

Bạn có lẽ nên làm điều gì đó như thế này:

var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';

EDIT: Để điều này năng động hơn, bạn có thể viết một hàm như mô tả bên dưới để lặp qua một đối tượng của các thuộc tính và giá trị của chúng và áp dụng chúng vào phần tử đích. For this to be more dynamic you could possibly write a function like the one below to loop over an object of attributes and their values and apply them to the target element.

Có vẻ như những gì bạn đang tìm kiếm để đạt được là khá khó khăn với Vanilla JavaScript, tôi sẽ xem xét sử dụng một số loại thư viện, có thể phản ứng, vì có vẻ như bạn đang tìm cách thay đổi dựa trên trạng thái.

// Function
function updateElement (element, attributes) {
  for (attr in attributes) {
    element.setAttribute(attr, attributes[attr]);
  }
}


// Use
var myNode = document.getElementById("myNode");
var attributes = {
    updated: true,
    foo: bar
};

updateElement(myNode, attributes);

Đã trả lời ngày 13 tháng 9 năm 2016 lúc 15:50Sep 13, 2016 at 15:50

Hướng dẫn replace outerhtml javascript - thay thế externalhtml javascript

NitsewnitsewNitsew

Phù vàng 3.5821 Huy hiệu vàng15 Huy hiệu đồng 201 gold badge15 silver badges20 bronze badges

2

Về cơ bản, bạn sẽ muốn biến đổi phần tử để thực hiện mỗi thay đổi riêng lẻ. Để thực hiện các thay đổi bạn đã cho thấy, bạn sẽ muốn làm như sau

document.getElementById("myNode").setAttribute('updated', true);
document.getElementById("myNode").setAttribute('foo', 'bar');
document.getElementById("myNode").textContent = 'hello world!!!';

Nếu bạn muốn sử dụng điều này trên bất kỳ yếu tố nào, bạn sẽ cần thay đổi đối số được truyền để getEuityByID để khớp với ID phần tử của bạn.

Sử dụng SetAttribution sẽ hoạt động cho hầu hết các thuộc tính và hoạt động theo định dạng setAttribution (AttributionName, AttributionValue). Để biết chi tiết về cách các tác phẩm của SetAttribute, tôi khuyên bạn nên xem xét các tài liệu MDN khi họ giải thích các caviats khá rộng rãi (https://developer.mozilla.org/en-us/docs/web/api/element/setattribution).

Để đặt nội thất của phần tử, bạn sẽ cần đặt thuộc tính bên trong nếu bạn đang thêm HTML hoặc thuộc tính TextContent nếu chỉ gán văn bản (hữu ích vì bạn không cần phải thoát HTML)

Đã trả lời ngày 13 tháng 9 năm 2016 lúc 15:49Sep 13, 2016 at 15:49

Waterfoulwaterfoulwaterfoul

3133 Huy hiệu bạc6 Huy hiệu Đồng3 silver badges6 bronze badges

2

Phân tích cú pháp XML hoặc HTML vào cây dom:

document.getElementById("myNode").setAttribute('updated', true);
document.getElementById("myNode").setAttribute('foo', 'bar');
document.getElementById("myNode").textContent = 'hello world!!!';
0
var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
2
attribute of the
var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
3 DOM interface gets the serialized HTML fragment describing the element including its descendants. It can also be set to replace the element with nodes parsed from the given string.

Thuộc tính

var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
2 của giao diện DOM
var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
3 có đoạn HTML được nối tiếp mô tả phần tử bao gồm cả hậu duệ của nó. Nó cũng có thể được đặt để thay thế phần tử bằng các nút được phân tích cú pháp từ chuỗi đã cho.

Để chỉ có được biểu diễn HTML của nội dung của một phần tử hoặc để thay thế nội dung của một phần tử, hãy sử dụng thuộc tính var el = document.getElementById("myNode"); el.setAttribute('updated', true); el.setAttribute('foo', 'bar'); el.textContent = 'new text'; 4 thay thế.

Giá trị

Đọc giá trị của var el = document.getElementById("myNode"); el.setAttribute('updated', true); el.setAttribute('foo', 'bar'); el.textContent = 'new text'; 2 trả về một chuỗi chứa tuần tự hóa HTML của var el = document.getElementById("myNode"); el.setAttribute('updated', true); el.setAttribute('foo', 'bar'); el.textContent = 'new text'; 6 và hậu duệ của nó. Đặt giá trị của var el = document.getElementById("myNode"); el.setAttribute('updated', true); el.setAttribute('foo', 'bar'); el.textContent = 'new text'; 2 thay thế phần tử và tất cả các hậu duệ của nó bằng một cây dom mới được xây dựng bằng cách phân tích phân tích var el = document.getElementById("myNode"); el.setAttribute('updated', true); el.setAttribute('foo', 'bar'); el.textContent = 'new text'; 8 được chỉ định.

Ngoại lệ

var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
9
// Function
function updateElement (element, attributes) {
  for (attr in attributes) {
    element.setAttribute(attr, attributes[attr]);
  }
}


// Use
var myNode = document.getElementById("myNode");
var attributes = {
    updated: true,
    foo: bar
};

updateElement(myNode, attributes);
0

Ném nếu một nỗ lực được thực hiện để đặt
var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
2 bằng chuỗi HTML không hợp lệ.

// Function
function updateElement (element, attributes) {
  for (attr in attributes) {
    element.setAttribute(attr, attributes[attr]);
  }
}


// Use
var myNode = document.getElementById("myNode");
var attributes = {
    updated: true,
    foo: bar
};

updateElement(myNode, attributes);
2
// Function
function updateElement (element, attributes) {
  for (attr in attributes) {
    element.setAttribute(attr, attributes[attr]);
  }
}


// Use
var myNode = document.getElementById("myNode");
var attributes = {
    updated: true,
    foo: bar
};

updateElement(myNode, attributes);
0

Ném nếu một nỗ lực được thực hiện để đặt var el = document.getElementById("myNode"); el.setAttribute('updated', true); el.setAttribute('foo', 'bar'); el.textContent = 'new text'; 2 trên một phần tử là một đứa trẻ trực tiếp của // Function function updateElement (element, attributes) { for (attr in attributes) { element.setAttribute(attr, attributes[attr]); } } // Use var myNode = document.getElementById("myNode"); var attributes = { updated: true, foo: bar }; updateElement(myNode, attributes); 5, chẳng hạn như // Function function updateElement (element, attributes) { for (attr in attributes) { element.setAttribute(attr, attributes[attr]); } } // Use var myNode = document.getElementById("myNode"); var attributes = { updated: true, foo: bar }; updateElement(myNode, attributes); 6.

Ví dụ

HTML

<div id="d">
  <p>Contentp>
  <p>Further Elaboratedp>
div>

JavaScript

const d = document.getElementById("d");
console.log(d.outerHTML);

// The string '

Content

Further Elaborated

'
// is written to the console window

Thay thế một nút bằng cách đặt thuộc tính Outerhtml

HTML

<div id="container">
  <div id="d">This is a div.div>
div>

JavaScript

const container = document.getElementById("container");
const d = document.getElementById("d");

console.log(container.firstElementChild.nodeName); // logs "DIV"

d.outerHTML = "

This paragraph replaced the original div.

"
; console.log(container.firstElementChild.nodeName); // logs "P" // The #d div is no longer part of the document tree, // the new paragraph replaced it.

Thay thế một nút bằng cách đặt thuộc tính Outerhtml

Ghi chú

const div = document.createElement("div");
div.outerHTML = "
test
"
; console.log(div.outerHTML); // output: "
"

Nếu phần tử không có phần tử cha, việc đặt thuộc tính

var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
2 của nó sẽ không thay đổi nó hoặc con cháu của nó. Nhiều trình duyệt cũng sẽ ném một ngoại lệ. Ví dụ:

const p = document.querySelector("p");
console.log(p.nodeName); // shows: "P"
p.outerHTML = "
This div replaced a paragraph.
"
; console.log(p.nodeName); // still "P";

Ngoài ra, trong khi phần tử sẽ được thay thế trong tài liệu, biến có thuộc tính

var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
2 được đặt vẫn sẽ giữ tham chiếu đến phần tử gốc:

var el = document.getElementById("myNode");

el.setAttribute('updated', true);
el.setAttribute('foo', 'bar');
el.textContent = 'new text';
0

Giá trị trả về sẽ chứa các thuộc tính thoát HTML:

Thông số kỹ thuật
Sự chỉ rõ
# dom-element-outerhtml

Dom phân tích và tuần tự hóa # DOM-Sement-Oliterhtml

Tính tương thích của trình duyệt web

Bảng BCD chỉ tải trong trình duyệt

  • Xem thêm
  • Tuần tự hóa cây Dom thành chuỗi XML:
    // Function
    function updateElement (element, attributes) {
      for (attr in attributes) {
        element.setAttribute(attr, attributes[attr]);
      }
    }
    
    
    // Use
    var myNode = document.getElementById("myNode");
    var attributes = {
        updated: true,
        foo: bar
    };
    
    updateElement(myNode, attributes);
    
    9
  • document.getElementById("myNode").setAttribute('updated', true);
    document.getElementById("myNode").setAttribute('foo', 'bar');
    document.getElementById("myNode").textContent = 'hello world!!!';
    
    1