Uncomment html code using javascript

后端 未结 5 1516
春和景丽
春和景丽 2020-12-31 15:51

Html tables with some commented tags. i just wanted to uncomment those tags. I have tried regex using javascript but problem is it removes entire commented line where as i

5条回答
  •  别那么骄傲
    2020-12-31 16:09

    There's a very useful little jquery plugin that does precisely this. I didn't write it, but it's open source and here's the code & attribution to original source:

    http://vistaprint.github.io/SkinnyJS/jquery.uncomment.html

    Works well on our site.

    Javascript is:

    (function ($) {
        $.fn.uncomment = function () {
            for (var i = 0, l = this.length; i < l; i++) {
                for (var j = 0, len = this[i].childNodes.length; j < len; j++) {
                    if (this[i].childNodes[j].nodeType === 8) {
                        var content = this[i].childNodes[j].nodeValue;
                        $(this[i].childNodes[j]).replaceWith(content);
                    }
                }
            }
        };
    })(jQuery);
    

    Just put your code into

    and call it with

     $(".commented-container").uncomment();
    

    We're using it to defer/exclude some image-heavy galleries for mobile users, to speed the page loads.

    Part of skinny.js.. which you can find here: http://vistaprint.github.io/SkinnyJS/

提交回复
热议问题