使用javascript取消注释html代码 [英] Uncomment html code using javascript
问题描述
< table>
< tr>
< td> ABCD< / td>
< td>逻辑< / td>
<! - < td> 26538568< / td> - >
< / tr>
< / table>
所以在上面的代码中,我只想取消注释<! - < ; TD> 26538568< TD> - >
。
请这是从网页抓取数据的一部分,所以我不能更改html代码。上面提到的表结构类似于我尝试提取数据的网页。 您可以通过使用DOM,而不将文档视为文本。例如,使用jQuery:
$('table tr')
.contents()
。 (函数(){返回this.data;})
>
这里有趣的是 .contents
,它返回所有节点,而不仅仅是元素 - 这包括文本节点和注释。
工作示例: http://jsfiddle.net/9Z5T5/2/
注意:我不确定跨浏览器是如何实现的。具体而言,不支持 node.data
。我已经在Firefox,Chrome和IE 10中测试过这些代码。
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 just wanted to uncomment those tags. Below sample html table with commented tags...
<table>
<tr>
<td>ABCD</td>
<td>Logic</td>
<!-- <td>26538568</td> -->
</tr>
</table>
So in above code i just want to uncomment <!-- <td>26538568<td> -->
.
Please this is part of data scraping from webpage, so i cannot change the html code. Above mentioned table structure is similar to web page from where i am trying extract the data.
You can do it by using the DOM, without treating the document as text. For example, using jQuery:
$('table tr')
.contents()
.filter(function(){return this.nodeType === 8;}) //get the comments
.replaceWith(function(){return this.data;})
The interesting bit here is .contents
, which returns all nodes, not just the elements - this includes text nodes and comments.
Working example: http://jsfiddle.net/9Z5T5/2/
Cautionary Note: I'm not sure how cross-browser is this. Specifically, it is possible node.data
isn't supported. I've tested this code in Firefox, Chrome, and IE 10.
这篇关于使用javascript取消注释html代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!