Hướng dẫn convert special characters javascript - chuyển đổi các ký tự đặc biệt javascript

Tôi biết có những thư viện ngoài kia, nhưng đây là một vài giải pháp cho trình duyệt. Chúng hoạt động tốt khi đặt chuỗi dữ liệu thực thể HTML vào các khu vực có thể chỉnh sửa của con người, nơi bạn muốn các ký tự được hiển thị, chẳng hạn như Textarea hoặc đầu vào [type = text].

Nội dung chính

  • Mã hóa và giải mã mọi thứ
  • Sử dụng một thư viện
  • Làm thế nào để bạn chuyển đổi các ký tự đặc biệt trong HTML?
  • Tôi có thể thoát khỏi các ký tự đặc biệt HTML trong JavaScript không?
  • Tôi có thể sử dụng các ký tự đặc biệt trong JavaScript không?
  • Escapehtml trong JavaScript là gì?

Tôi thêm câu trả lời này vì tôi phải hỗ trợ các phiên bản cũ hơn của IE và tôi cảm thấy rằng nó kết thúc một vài ngày để nghiên cứu và thử nghiệm. Tôi hy vọng ai đó thấy điều này hữu ích.

Đầu tiên, đây là dành cho các trình duyệt hiện đại hơn bằng cách sử dụng jQuery, xin lưu ý rằng điều này không nên được sử dụng nếu bạn phải hỗ trợ các phiên bản IE trước 10 [7, 8 hoặc 9] của văn bản.

if [!String.prototype.HTMLDecode] {
    String.prototype.HTMLDecode = function [] {
            var str = this.toString[],
            $decoderEl = $[''];

        str = $decoderEl.html[str]
            .text[]
            .replace[//gi, "\r\n"];

        $decoderEl.remove[];

        return str;
    };
}

Điều này tiếp theo dựa trên công việc của Kennebec ở trên, với một số khác biệt chủ yếu là vì lợi ích của các phiên bản IE cũ hơn. Điều này không yêu cầu jquery, nhưng vẫn yêu cầu trình duyệt.

if [!String.prototype.HTMLDecode] {
    String.prototype.HTMLDecode = function [] {
        var str = this.toString[],
            //Create an element for decoding            
            decoderEl = document.createElement['p'];

        //Bail if empty, otherwise IE7 will return undefined when 
        //OR-ing the 2 empty strings from innerText and textContent
        if [str.length == 0] {
            return str;
        }

        //convert newlines to  to save them
        str = str.replace[/[[\r\n]|[\r]|[\n]]/gi, " 
"]; decoderEl.innerHTML = str; /* We use innerText first as IE strips newlines out with textContent. There is said to be a performance hit for this, but sometimes correctness of data [keeping newlines] must take precedence. */ str = decoderEl.innerText || decoderEl.textContent; //clean up the decoding element decoderEl = null; //replace back in the newlines return str.replace[//gi, "\r\n"]; }; } /* Usage: var str = ">"; return str.HTMLDecode[]; returned value: [String] > */
6,
if [!String.prototype.HTMLDecode] {
    String.prototype.HTMLDecode = function [] {
        var str = this.toString[],
            //Create an element for decoding            
            decoderEl = document.createElement['p'];

        //Bail if empty, otherwise IE7 will return undefined when 
        //OR-ing the 2 empty strings from innerText and textContent
        if [str.length == 0] {
            return str;
        }

        //convert newlines to  to save them
        str = str.replace[/[[\r\n]|[\r]|[\n]]/gi, " 
"]; decoderEl.innerHTML = str; /* We use innerText first as IE strips newlines out with textContent. There is said to be a performance hit for this, but sometimes correctness of data [keeping newlines] must take precedence. */ str = decoderEl.innerText || decoderEl.textContent; //clean up the decoding element decoderEl = null; //replace back in the newlines return str.replace[//gi, "\r\n"]; }; } /* Usage: var str = ">"; return str.HTMLDecode[]; returned value: [String] > */
8 sẵn sàng sử dụng.

Mã hóa và giải mã mọi thứ

Sử dụng một thư viện

Làm thế nào để bạn chuyển đổi các ký tự đặc biệt trong HTML?

Tôi có thể thoát khỏi các ký tự đặc biệt HTML trong JavaScript không?Note that the encode method, will convert every single character into its html character.

Tôi có thể sử dụng các ký tự đặc biệt trong JavaScript không?only those weird characters that broke your html [,/,\ etc] keep reading and don't use this method, otherwise this snippet comes in handy.

[function[window]{
	window.htmlentities = {
		/**
		 * Converts a string to its html characters completely.
		 *
		 * @param {String} str String with unescaped HTML characters
		 **/
		encode : function[str] {
			var buf = [];
			
			for [var i=str.length-1;i>=0;i--] {
				buf.unshift[['&#', str[i].charCodeAt[], ';'].join['']];
			}
			
			return buf.join[''];
		},
		/**
		 * Converts an html characterSet into its original character.
		 *
		 * @param {String} str htmlSet entities
		 **/
		decode : function[str] {
			return str.replace[/&#[\d+];/g, function[match, dec] {
				return String.fromCharCode[dec];
			}];
		}
	};
}][window];

Escapehtml trong JavaScript là gì?

Tôi thêm câu trả lời này vì tôi phải hỗ trợ các phiên bản cũ hơn của IE và tôi cảm thấy rằng nó kết thúc một vài ngày để nghiên cứu và thử nghiệm. Tôi hy vọng ai đó thấy điều này hữu ích.

htmlentities.encode["Hello, this is a test stríng > < with characters that could break html. Therefore we convert it to its html characters."];
// Output
"Hello, this is a test stríng > < with characters that could break html. Therefore we convert it to its html characters."

Đầu tiên, đây là dành cho các trình duyệt hiện đại hơn bằng cách sử dụng jQuery, xin lưu ý rằng điều này không nên được sử dụng nếu bạn phải hỗ trợ các phiên bản IE trước 10 [7, 8 hoặc 9] của văn bản.

htmlentities.decode["Hello, this is a test stríng > < with characters that could break html. Therefore we convert it to its html characters."];
// Output
"Hello, this is a test stríng > < with characters that could break html. Therefore we convert it to its html characters."

Điều này tiếp theo dựa trên công việc của Kennebec ở trên, với một số khác biệt chủ yếu là vì lợi ích của các phiên bản IE cũ hơn. Điều này không yêu cầu jquery, nhưng vẫn yêu cầu trình duyệt. feel free to copy every single function and include it in your project as you wish.

Sử dụng một thư viện

Làm thế nào để bạn chuyển đổi các ký tự đặc biệt trong HTML?

Tôi có thể thoát khỏi các ký tự đặc biệt HTML trong JavaScript không? [for “HTML entities”] is a robust HTML entity encoder/decoder written in JavaScript. It supports all standardized named character references as per HTML, handles ambiguous ampersands and other edge cases just like a browser would, has an extensive test suite, and contrary to many other JavaScript solutions, he handles astral Unicode symbols just fine. An online demo is available.

Tôi có thể sử dụng các ký tự đặc biệt trong JavaScript không?

Escapehtml trong JavaScript là gì?

Tôi thêm câu trả lời này vì tôi phải hỗ trợ các phiên bản cũ hơn của IE và tôi cảm thấy rằng nó kết thúc một vài ngày để nghiên cứu và thử nghiệm. Tôi hy vọng ai đó thấy điều này hữu ích.

Đầu tiên, đây là dành cho các trình duyệt hiện đại hơn bằng cách sử dụng jQuery, xin lưu ý rằng điều này không nên được sử dụng nếu bạn phải hỗ trợ các phiên bản IE trước 10 [7, 8 hoặc 9] của văn bản.,
if [!String.prototype.HTMLDecode] {
    String.prototype.HTMLDecode = function [] {
        var str = this.toString[],
            //Create an element for decoding            
            decoderEl = document.createElement['p'];

        //Bail if empty, otherwise IE7 will return undefined when 
        //OR-ing the 2 empty strings from innerText and textContent
        if [str.length == 0] {
            return str;
        }

        //convert newlines to  to save them
        str = str.replace[/[[\r\n]|[\r]|[\n]]/gi, " 
"]; decoderEl.innerHTML = str; /* We use innerText first as IE strips newlines out with textContent. There is said to be a performance hit for this, but sometimes correctness of data [keeping newlines] must take precedence. */ str = decoderEl.innerText || decoderEl.textContent; //clean up the decoding element decoderEl = null; //replace back in the newlines return str.replace[//gi, "\r\n"]; }; } /* Usage: var str = ">"; return str.HTMLDecode[]; returned value: [String] > */
2,
if [!String.prototype.HTMLDecode] {
    String.prototype.HTMLDecode = function [] {
        var str = this.toString[],
            //Create an element for decoding            
            decoderEl = document.createElement['p'];

        //Bail if empty, otherwise IE7 will return undefined when 
        //OR-ing the 2 empty strings from innerText and textContent
        if [str.length == 0] {
            return str;
        }

        //convert newlines to  to save them
        str = str.replace[/[[\r\n]|[\r]|[\n]]/gi, " 
"]; decoderEl.innerHTML = str; /* We use innerText first as IE strips newlines out with textContent. There is said to be a performance hit for this, but sometimes correctness of data [keeping newlines] must take precedence. */ str = decoderEl.innerText || decoderEl.textContent; //clean up the decoding element decoderEl = null; //replace back in the newlines return str.replace[//gi, "\r\n"]; }; } /* Usage: var str = ">"; return str.HTMLDecode[]; returned value: [String] > */
4, and
if [!String.prototype.HTMLDecode] {
    String.prototype.HTMLDecode = function [] {
        var str = this.toString[],
            //Create an element for decoding            
            decoderEl = document.createElement['p'];

        //Bail if empty, otherwise IE7 will return undefined when 
        //OR-ing the 2 empty strings from innerText and textContent
        if [str.length == 0] {
            return str;
        }

        //convert newlines to  to save them
        str = str.replace[/[[\r\n]|[\r]|[\n]]/gi, " 
"]; decoderEl.innerHTML = str; /* We use innerText first as IE strips newlines out with textContent. There is said to be a performance hit for this, but sometimes correctness of data [keeping newlines] must take precedence. */ str = decoderEl.innerText || decoderEl.textContent; //clean up the decoding element decoderEl = null; //replace back in the newlines return str.replace[//gi, "\r\n"]; }; } /* Usage: var str = ">"; return str.HTMLDecode[]; returned value: [String] > */
4

Điều này tiếp theo dựa trên công việc của Kennebec ở trên, với một số khác biệt chủ yếu là vì lợi ích của các phiên bản IE cũ hơn. Điều này không yêu cầu jquery, nhưng vẫn yêu cầu trình duyệt.

Tìm hiểu cách mã hóa và giải mã cho các thực thể HTML Một chuỗi bằng JavaScript.the algorithm described in section 12.2.4.69 of the HTML spec.

if [!String.prototype.HTMLDecode] {
    String.prototype.HTMLDecode = function [] {
        var str = this.toString[],
            //Create an element for decoding            
            decoderEl = document.createElement['p'];

        //Bail if empty, otherwise IE7 will return undefined when 
        //OR-ing the 2 empty strings from innerText and textContent
        if [str.length == 0] {
            return str;
        }

        //convert newlines to 

Bài Viết Liên Quan

Chủ Đề