Hướng dẫn convert html input element to string - chuyển đổi phần tử đầu vào html thành chuỗi

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.

Hướng dẫn convert html input element to string - chuyển đổi phần tử đầu vào html thành chuỗi

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

  1. Outerhtml - thuộc tính JavaScript
  2. 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 = "<"+element.tagName;
for (var i = 0; i < element.attributes.length; i++) {
    var attrib = element.attributes[i];
    openTag += " "+attrib.name + "=" + attrib.value;
}
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

Hướng dẫn convert html input element to string - chuyển đổi phần tử đầu vào html thành chuỗi

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 = 'http://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;
}
0

Và 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

Hướng dẫn convert html input element to string - chuyển đổi phần tử đầu vào html thành chuỗi

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

Làm cách nào để biến một phần tử thành một chuỗi?

Để chuyển đổi htmlelement thành một chuỗi với javascript, chúng ta có thể sử dụng thuộc tính outerhtml. const phần tử = tài liệu. getEuityByid ("New-Element-1"); const ElementHtml = phần tử.use the outerHTML property. const element = document. getElementById("new-element-1"); const elementHtml = element.

Làm cách nào để chuyển đổi thẻ HTML thành văn bản đơn giản?

Thay thế (/]*>/g, '') Phương pháp này là một cách đơn giản và hiệu quả để xóa các thẻ khỏi văn bản.Phương pháp này sử dụng phương thức chuỗi.Thay thế (giá trị cũ, giá trị mới) thay thế các giá trị thẻ HTML bằng chuỗi trống. This method is a simple and efficient way to remove the tags from the text. This method uses the string method . replace(old value,new value) which replaces the HTML tag values with the empty string.

Làm thế nào để bạn chuyển đổi một chuỗi trong HTML?

Cách đơn giản nhất để làm điều này là tạo một phần tử, chèn chuỗi vào bên trong, sau đó trả về phần tử..create an element, insert the string into with innerHTML , then return the element. /** * Convert a template string into HTML DOM nodes * @param {String} str The template string * @return {Node} The template HTML */ var stringToHTML = function (str) { var dom = document.

Làm thế nào để bạn chuyển đổi một cái gì đó thành một chuỗi trong javascript?

Chuyển đổi các giá trị thành các giá trị chuỗi có thể được chuyển đổi rõ ràng thành các chuỗi bằng cách gọi chuỗi () hoặc n.toString ().Với hàm chuỗi (), hãy chuyển đổi giá trị boolean thành một chuỗi bằng cách chuyển giá trị true vào các tham số cho chuỗi ().Khi chúng ta làm điều này, chuỗi "True" sẽ được trả lại.Values can be explicitly converted to strings by calling either String() or n. toString() . With the String() function, let's convert a Boolean value to a string by passing the value true into the parameters for String() . When we do this, the string literal "true" will be returned.