最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to close html tags in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

I'm taking html content out of my database and displaying it on a page.

Unfortunately these pages often have unclosed html tags and cause problems later when the page is rendered.

I was wondering if there is a JavaScript implementation of something like tidy or htmlpurifier.

Basically some software which can preferably close html tags in a string.

Edit: I'm not in a browser environment (node.js)

I'm taking html content out of my database and displaying it on a page.

Unfortunately these pages often have unclosed html tags and cause problems later when the page is rendered.

I was wondering if there is a JavaScript implementation of something like tidy or htmlpurifier.

Basically some software which can preferably close html tags in a string.

Edit: I'm not in a browser environment (node.js)

Share Improve this question asked Jan 19, 2012 at 21:18 AaronAaron 1,58311 silver badges10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

As you tagged your question with node.js:

npm install tidy

Something like this could work:

function tidy(htmldata) {
    var d = document.createElement('div');
    d.innerHTML = htmldata;
    return d.innerHTML;
}

http://code.google./p/google-caja/wiki/JsHtmlSanitizer balances tags.

发布评论

评论列表(0)

  1. 暂无评论