is it possible to remove an html comment from dom using jquery

后端 未结 5 1820
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 01:51

just wondering if there was a way to remove an html comment using jquery.


than

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 02:14

    Try this:

    $('*').contents().each(function() {
        if(this.nodeType === Node.COMMENT_NODE) {
            $(this).remove();
        }
    });
    

    EDIT: This removes the elements from the DOM. Browsers often store a copy of the original page source that is accessible through a menu item. This doesn't get updated.

    If you want to hide your comments, you could always insert your entire HTML markup (with comments) into the DOM using javascript. The javascript could, of course, be viewed, but it is a step removed from the first place people would look.

提交回复
热议问题