获取HTML评论标记内的文字? [英] Get text inside HTML comment tag?

查看:132
本文介绍了获取HTML评论标记内的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML:

<!--
<option value="HVAC">HVAC</option>
<option value="Cooling">|---Cooling</option>
<option value="Heating">|---Heating</option>
-->
....

我使用jQuery的get方法动态获取此文件并将其存储在一个名为的字符串变量load_types

I fetch this file dynamically using jQuery's get method and store it in a string variable named load_types.

如何删除HTML注释标记以及它们之外的所有内容?我只想要内部HTML:

How can I strip the HTML comment tags and everything outside of them? I only want the inside HTML:

<option value="HVAC">HVAC</option>
<option value="Cooling">|---Cooling</option>
<option value="Heating">|---Heating</option>

我试图使用这里的解决方案但没有任何工作正常 - 我只是得到 null 作为匹配。

I tried to use the solutions here but nothing worked properly--I just get null as a match.

感谢您的帮助!

推荐答案

不使用正则表达式解析HTML 。你可以使用以下代码:

Please never use regex to parse HTML. You can use the following instead:

var div = $("<div>").html(load_types),
    comment = div.contents().filter(function() {
        return this.nodeType === 8;
    }).get(0);

console.log(comment.nodeValue);

DEMO: http://jsfiddle.net/HHtW7/

这篇关于获取HTML评论标记内的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆