Khi tôi chạy mã này, nó hiển thị cho tôi màn hình trống nhưng khi tôi cập nhật mã bằng công cụ nhà phát triển trong chrome thì nó sẽ hiển thị dữ liệu. Vui lòng giúp giải thích lý do tại sao nó hiển thị khi tôi cập nhật mã bằng công cụ nhà phát triển của chrome, Có phải do DOM tại trình duyệt chạy lại hay không, nếu có thì tại sao không ở lần đầu tiên nó hiển thị. Điều này có xảy ra do ngoại lệ không.thêm đối tượng động bên ngoài vào svg bằng cách sử dụng javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<svg id="t">
<g>
<text x="10" y="10">hello</text>
</g>
</svg>
<script>
var s = document.getElementById('t');
var g = s.childNodes[1];
console.log(g.childNodes[1].remove());
var foreign = document.createElementNS('http://www.w3.org/2000/svg',"foreignObject");
foreign.setAttribute('width', 500);
foreign.setAttribute('height', 150);
var txt = document.createElementNS('http://www.w3.org/2000/svg', 'text');
txt.setAttribute('x', '10');
txt.setAttribute('y', '10');
var t = document.createTextNode("This is a paragraph.");
txt.appendChild(t);
foreign.appendChild(txt);
g.appendChild(foreign);
</script>
</body>
</html>
@JabranSaeed, Robert longson là kinh điển. – Kaiido