从DOM中删除HTML注释

时间:2012-03-20 11:50:48

标签: php javascript html comments

所以基本上,我试图实现的是通过PHP在html注释中输出数据,因此它不会被渲染。 然后使用javascript删除评论,使其在屏幕上呈现,但在视图页面源中,评论必须仍然存在。

我尝试了其他stackoverflow帖子中提到的一些解决方案,但没有做到这一点。 任何人的想法?

例如:如果你查看facebook的源代码,它们的内容就在评论中,但它已经被渲染了。

    <script>big_pipe.onPageletArrive({"phase":1,"id":"pagelet_composer","css":["sbVQp"],"bootloadable":{"legacy:Composer":{"resources":["Q6HMA","sbVQp"]},"legacy:DataSource":{"resources":["Q6HMA"]},"legacy:dom":{"resources":["Q6HMA"]},"legacy:control-textarea":{"resources":["Q6HMA","IRaZg","sbVQp"]}},"resource_map":{"IRaZg":{"type":"js","src":"http:\/\/static.ak.fbcdn.net\/rsrc.php\/v1\/yM\/r\/pc-N6ldw6Ia.js"}},"js":["Q6HMA"],"jscc_map":"({\"j98xJNAPg55OWGAly21\":function(){return new DataSource({\"maxResults\":5,\"queryData\":{\"viewer\":100003192277539,\"filter\":[\"page\",\"app\"],\"context\":\"topics\",\"rsp\":\"mentions\"},\"queryEndpoint\":\"\\\/ajax\\\/typeahead\\\/search.php\",\"bootstrapData\":{\"viewer\":100003192277539,\"filter\":[\"user\",\"friendlist\",\"page\",\"app\",\"group\",\"event\"],\"options\":[\"friends_only\",\"nm\",\"include_subscribed_lists\"],\"token\":\"v7\",\"context\":\"mentions\",\"rsp\":\"mentions\"},\"bootstrapEndpoint\":\"\\\/ajax\\\/typeahead\\\/first_degree.php\",\"token\":\"1332238929-7\"});},\"j98xJNAPg55OWGAly22\":function(){return new Composer($(\"uwgh9l_7\"), {\"xhpc\":\"composerTourStart\",\"endpoint\":\"\\\/ajax\\\/updatestatus.php\",\"formType\":1,\"placeholder\":\"What's on your mind?\",\"buttonLabel\":\"Post\",\"autoscrape\":true,\"barContent\":\"\\u003Cspan class=\\\"uiComposerTagControls friendTaggerIcon\\\">\\u003C\\\/span>\\u003Cspan class=\\\"uiComposerTagControls placeTaggerIcon\\\">\\u003C\\\/span>\",\"disableCache\":true,\"lazyEndpoint\":\"\\\/ajax\\\/metacomposer\\\/attachment\\\/status\\\/status.php\"}, true, JSCC.get('j98xJNAPg55OWGAly21'));},\"j98xJNAPg55OWGAly23\":function(){return new MentionsInput($(\"uwgh9l_8\"));},\"j98xJNAPg55OWGAly24\":function(){return new MetaComposerMessageBox();},\"j98xJNAPg55OWGAly25\":function(){return new TypeaheadMetrics({\"extraData\":{\"event_name\":\"mentions\"}});},\"j98xJNAPg55OWGAly26\":function(){return new Typeahead(JSCC.get('j98xJNAPg55OWGAly21'), {node_id: \"\", ctor: \"ContextualTypeaheadView\", options: {\"autoSelect\":true,\"renderer\":\"compact\"}}, {ctor: \"TypeaheadAreaCore\", options: {}}, $(\"uwgh9l_12\"))}})","onload":["JSCC.get('j98xJNAPg55OWGAly22').init(JSCC.get('j98xJNAPg55OWGAly23'));","JSCC.get('j98xJNAPg55OWGAly24').init($(\"uwgh9l_11\"), JSCC.get('j98xJNAPg55OWGAly23'));"],"content":{"pagelet_composer":{"container_id":"uwgh9l_14"}}});</script>



<code class="hidden_elem" id="uwgh9l_15"><!-- <ul id="boulder_fixed_header" class="uiStream"><li class="mts uiStreamHeader"><span class="plm uiStreamHeaderText fss fwb"></span><div class="uiStreamHeaderChronologicalForm"><div class="uiSelector inlineBlock uiSelectorRight uiSelectorNormal uiSelectorDynamicLabel"><div class="wrap"><a class="uiSelectorButton uiButton" role="button" href="#" aria-haspopup="1" data-length="30" rel="toggle"><span class="uiButtonText">SORT</span></a><div class="uiSelectorMenuWrapper uiToggleFlyout"><div role="menu" class="uiMenu uiSelectorMenu"><ul class="uiMenuInner"><li class="uiMenuItem uiMenuItemRadio uiSelectorOption checked" data-label="SORT"><a class="itemAnchor" role="menuitemradio" tabindex="0" aria-checked="true" href="/?sk=h_nor"><span class="itemLabel fsm">Top Stories</span></a></li><li class="uiMenuItem uiMenuItemRadio uiSelectorOption" data-label="SORT: MOST RECENT"><a class="itemAnchor" role="menuitemradio" tabindex="-1" aria-checked="false" href="/?sk=h_chr"><span class="itemLabel fsm">Most Recent</span></a></li></ul></div></div></div><select><option value=""></option><option value="h_nor" selected="1">SORT</option><option value="h_chr">SORT: MOST RECENT</option></select></div></div></li></ul><div id="pagelet_stream_masher" data-referrer="pagelet_stream_masher"></div><div class="UIIntentionalStream UIStream"  id="c4f68733a3e65c9c49002913"><ul class="uiList uiStream uiStreamHomepage translateParent UIIntentionalStream_Content" id="home_stream"></ul><div ><div class="UIIntentionalStream_Error"><div class="pam uiBoxRed"><div class="fsl fwb fcb">This stream is unavailable at this time. Please try again soon.</div></div></div></div><div ><div id="pagelet_stream_pager"></div></div></div> --></code>

他们使用一个名为bigpipe的系统实时和更快的界面,但我不想使用实时系统(不是现在)。 所以我试着做同样的事情!

4 个答案:

答案 0 :(得分:3)

找到包含评论的元素(例如,使用document.getElementBydId),并执行此操作以提取评论中的内容:

element.innerHTML = element.innerHTML.replace(/<!--((.|\n|\r)*?)-->/g, '$1');

答案 1 :(得分:1)

简单使用css并将其设置为display:none;visibility : hidden; 如果它是出于seo目的,那么可以在这里阅读一篇文章 http://3n9.org/articles/css-hiding.html

和tbh我真的不明白你的要求...因为评论已经没有在屏幕上呈现所以我想你隐藏html onload

答案 2 :(得分:0)

将“已注释”代码包装在div中...并切换可见性(如下所示):

function toggleVisibility(var v)
{
    var vis="hidden";

    if (v) vis="visible";

    document.getElementById('yourDiv').style.visibility = vis;
}

答案 3 :(得分:-2)

您必须从页面获取所有节点,然后在它们中循环查找nodeType。检查注释,然后在父节点上使用removeChild属性。