Tôi sẽ tạo một phần tử XML trong JavaScript để trao đổi dữ liệu với phía máy chủ. Tôi thấy tôi có thể làm điều đó với
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
2. Nhưng tôi không biết làm thế nào để chuyển đổi nó thành chuỗi. Có API nào trong trình duyệt để làm cho nó dễ dàng hơn không? Hoặc có thư viện JS nào bao gồm API này không?EDIT: Tôi thấy rằng trình duyệt API XMLSerializer, nó phải là cách đúng đắn để tuần tự hóa thành chuỗi. I found that browser API XMLSerializer, it should be the right way to serialize to string.
Montrealist
5.50210 Huy hiệu vàng45 Huy hiệu bạc62 Huy hiệu Đồng10 gold badges45 silver badges62 bronze badges
hỏi ngày 19 tháng 3 năm 2010 lúc 2:21Mar 19, 2010 at 2:21
1
Thuộc tính phần tử
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
3 [Lưu ý: Được hỗ trợ bởi Firefox sau phiên bản 11] trả về HTML của toàn bộ phần tử.Thí dụ
Hello world.
Tương tự, bạn có thể sử dụng
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
4 để có được HTML chứa trong một phần tử nhất định hoặc document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
5 để có được văn bản bên trong một phần tử [đánh dấu Sans HTML].Xem thêm
- Outerhtml - thuộc tính JavaScript
- Tham khảo JavaScript - Các yếu tố
Đã trả lời ngày 19 tháng 3 năm 2010 lúc 2:32Mar 19, 2010 at 2:32
gpmcadamgpmcadamgpmcadam
5,8962 Huy hiệu vàng30 Huy hiệu bạc35 Huy hiệu Đồng2 gold badges30 silver badges35 bronze badges
3
Bạn có thể nhận được 'bên ngoài-HTML' bằng cách nhân bản phần tử, thêm nó vào một container trống, 'ngoài sân khấu' và đọc bên trong của container.
Ví dụ này có một tham số thứ hai tùy chọn.
Gọi tài liệu.gethtml [phần tử, true] để bao gồm hậu duệ của phần tử.
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
Đã trả lời ngày 19 tháng 3 năm 2010 lúc 3:04Mar 19, 2010 at 3:04
Kennebeckennebeckennebec
101K31 Huy hiệu vàng104 Huy hiệu bạc126 Huy hiệu đồng31 gold badges104 silver badges126 bronze badges
2
Giả sử phần tử của bạn là toàn bộ
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
6. Bạn có thể chuyển đổi nó thành một chuỗi theo cách này:String this way:const htmlTemplate = ``;
const domparser = new DOMParser[];
const doc = domparser.parseFromString[htmlTemplate, "text/html"]; // [object HTMLDocument]
const doctype = '';
const html = doc.documentElement.outerHTML;
console.log[doctype + html];
Đã trả lời ngày 6 tháng 8 năm 2020 lúc 18:53Aug 6, 2020 at 18:53
Topvovatopvovatopvova
511 huy hiệu bạc6 Huy hiệu đồng1 silver badge6 bronze badges
Có một thuộc tính
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
7 và thuộc tính document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
8 cũng vậy:var element = document.getElementById["wtv"];
var openTag = "";
alert[openTag];
Xem thêm Làm thế nào để lặp lại thông qua tất cả các thuộc tính trong một phần tử HTML? [Tôi đã làm!]
Để có được nội dung giữa các thẻ mở và gần, bạn có thể sử dụng
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
4 nếu bạn không muốn lặp lại tất cả các yếu tố con ...alert[element.innerHTML];
... và sau đó lấy lại thẻ gần với
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
7.var closeTag = "";
alert[closeTag];
Đã trả lời ngày 19 tháng 3 năm 2010 lúc 2:30Mar 19, 2010 at 2:30
3
Cách dễ dàng nhất để làm là sao chép bên trong phần tử đó thành biến TMP và làm cho nó trống, sau đó nối phần tử mới và sau đó sao chép lại biến TMP trở lại. Dưới đây là một ví dụ tôi đã sử dụng để thêm tập lệnh jQuery vào đầu đầu.
var imported = document.createElement['script'];
imported.src = '//code.jquery.com/jquery-1.7.1.js';
var tmpHead = document.head.innerHTML;
document.head.innerHTML = "";
document.head.append[imported];
document.head.innerHTML += tmpHead;
Thật đơn giản :]
Đã trả lời ngày 19 tháng 5 năm 2017 lúc 10:12May 19, 2017 at 10:12
Tôi đã sử dụng góc cạnh, và cần điều tương tự, và hạ cánh vào bài này.
@ViewChild['myHTML', {static: false}] _html: ElementRef;
this._html.nativeElement;
Đã trả lời ngày 11 tháng 12 năm 2019 lúc 13:00Dec 11, 2019 at 13:00
JnrjnrJnr
1.3182 Huy hiệu vàng19 Huy hiệu bạc32 Huy hiệu Đồng2 gold badges19 silver badges32 bronze badges
Điều này có thể không áp dụng cho trường hợp của mọi người, nhưng khi trích xuất từ XML, tôi đã gặp vấn đề này, điều mà tôi đã giải quyết với điều này.
function grab_xml[what]{
var return_xml =null;
$.ajax[{
type: "GET",
url: what,
success:function[xml]{return_xml =xml;},
async: false
}];
return[return_xml];
}
Sau đó, lấy XML:
var sector_xml=grab_xml["p/sector.xml"];
var tt=$[sector_xml].find["pt"];
Sau đó, tôi thực hiện chức năng này để trích xuất dòng XML, khi tôi cần đọc từ tệp XML, chứa các thẻ HTML.
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
0Và bây giờ để kết luận:
document.getHTML= function[who, deep]{
if[!who || !who.tagName] return '';
var txt, ax, el= document.createElement["div"];
el.appendChild[who.cloneNode[false]];
txt= el.innerHTML;
if[deep]{
ax= txt.indexOf['>']+1;
txt= txt.substring[0, ax]+who.innerHTML+ txt.substring[ax];
}
el= null;
return txt;
}
1
Lỗ hổng
1871 Huy hiệu bạc11 Huy hiệu đồng1 silver badge11 bronze badges
Đã trả lời ngày 8 tháng 5 năm 2013 lúc 15:39May 8, 2013 at 15:39
MiguelmiguelMiguel
3.1132 huy hiệu vàng29 Huy hiệu bạc28 Huy hiệu đồng2 gold badges29 silver badges28 bronze badges