Chuỗi thoát để sử dụng trong HTML Mô -đun này xuất một hàm duy nhất, Cài đặtĐây là mô -đun Node.js có sẵn thông qua sổ đăng ký NPM. Cài đặt được thực hiện bằng lệnh $ npm install escape-html APIEscapeHtml (chuỗi)Thoát các ký tự đặc biệt trong chuỗi văn bản đã cho, sao cho nó có thể được nội suy trong nội dung HTML. Hàm này sẽ thoát khỏi các ký tự sau: var escapeHtml = require('escape-html') // example values var desc = 'I <b>think</b> this is good.' var fullName = 'John "Johnny" Smith' // example passing in text into a html attribute console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">') // -> '<input name="full_name" value="John "Johnny" Smith">' // example passing in text in html body console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>') // -> '<textarea name="desc">I <b>think</b> this is good.</textarea>'0 và var escapeHtml = require('escape-html') // example values var desc = 'I <b>think</b> this is good.' var fullName = 'John "Johnny" Smith' // example passing in text into a html attribute console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">') // -> '<input name="full_name" value="John "Johnny" Smith">' // example passing in text in html body console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>') // -> '<textarea name="desc">I <b>think</b> this is good.</textarea>'1. Lưu ý rằng giá trị thoát chỉ phù hợp để được nội suy vào HTML vì nội dung văn bản của các phần tử trong đó thẻ không có cơ chế thoát khác nhau (ví dụ, nó không thể được đặt bên trong var escapeHtml = require('escape-html') // example values var desc = 'I <b>think</b> this is good.' var fullName = 'John "Johnny" Smith' // example passing in text into a html attribute console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">') // -> '<input name="full_name" value="John "Johnny" Smith">' // example passing in text in html body console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>') // -> '<textarea name="desc">I <b>think</b> this is good.</textarea>'2 or var escapeHtml = require('escape-html') // example values var desc = 'I <b>think</b> this is good.' var fullName = 'John "Johnny" Smith' // example passing in text into a html attribute console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">') // -> '<input name="full_name" value="John "Johnny" Smith">' // example passing in text in html body console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>') // -> '<textarea name="desc">I <b>think</b> this is good.</textarea>'3, for example, as those content bodies are not HTML, but CSS and JavaScript, respectively; these are known as "raw text elements" in the HTML standard). Lưu ý Khi sử dụng giá trị thoát trong thẻ, nó chỉ phù hợp là giá trị của một thuộc tính, trong đó giá trị được trích dẫn bằng ký tự trích dẫn kép ( Thí dụHàm var escapeHtml = require('escape-html') // example values var desc = 'I <b>think</b> this is good.' var fullName = 'John "Johnny" Smith' // example passing in text into a html attribute console.dir('<input name="full_name" value="' + escapeHtml(fullName) + '">') // -> '<input name="full_name" value="John "Johnny" Smith">' // example passing in text in html body console.dir('<textarea name="desc">' + escapeHtml(desc) + '</textarea>') // -> '<textarea name="desc">I <b>think</b> this is good.</textarea>' Điểm chuẩn
Giấy phépMIT 1.0.3 & nbsp; • & nbsp; public & nbsp; • & nbsp; xuất bản 7 năm trướcPublic • Published 7 years ago
Chuỗi thoát để sử dụng trong HTML var escape = require('escape-html'); var html = escape('foo & bar'); Điểm chuẩn
Giấy phépMIT |