Check if HTML snippet is valid with Javascript

前端 未结 7 1967
忘了有多久
忘了有多久 2020-11-29 05:03

I need a reliable Javascript library / function to check if a HTML snippet is valid that I can call from my code. For example, it should check that opened tags and quotation

7条回答
  •  执念已碎
    2020-11-29 05:43

    Well, this code:

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

    This will "correct" malformed HTML to the best of the browser's ability. If that's helpful to you, it's a lot easier than trying to validate HTML.

提交回复
热议问题