过滤一个html页面,所以只有某些标签显示
Filter a html page so only certain tags are showing
我有以下html代码
<html>
<body>
<div id='content'>
<h1>afsdsdsdf</h1>
<p>sdfsdfsdf</p>
<h2>sfddfdf</h2>
</body>
</html>
我怎样才能使它只输出<h1>afsdsdsdf</h1>
<h2>sfddfdf</h2>
我的代码atm
var allhtml = ($('#content').html());
document.getElementById('JumpLinks').innerHTML = allhtml;
试试下面的代码:
function textChange ( event )
{
if ( event . keyCode !== 8 && event . keyCode !== 9 && event . keyCode !== 13 && ( event . keyCode < 32 || event . keyCode > 127 ) ) { return; }
var newText = [];
document . getElementById ( "input" ) . value . replace ( /<('s*)?h([1-6])('s+)(['s'w-='"']*[^'s])?('s*)?>('s*)?(['s'w-='"']*[^'s])('s*)?<('s*)?'/('s*)?h([1-6])('s*)?>/gi, function ( match, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, offset, string ) { newText . push ( "<h" + p2 + ( p4 . length === 0 ? "" : " " ) + p4 . toLowerCase () + ">" + p7 + "</h" + p11 + ">" ); } );
// replace unnecesary whitespaces and case-insensitive tags: "h1", "h2", "h3", "h4", "h5" and "h6" and replace all text outside this tags
// Example changes: "<hm>Fake</hm>Another text< H2 ABC='CBA' > Test 2 < / h2 > ... other ..." to: "<h2 abc='cba'>Test 2</h2>"
// g - means global ( recursive ) search, i - means case-insensitive search
document . getElementById ( "output" ) . value = newText . join ( "'n" );
}
RegExp的解释:
< <=> match the character: "<" literally
('s+) <=> match any whitespace one or more times
h <=> match the character: "h" literally
([1-6]) <=> match single characters from list: "1", "2", "3", "4", "5", "6" literally
('s*)? <=> match ( if ) any whitespace
(['s'w-='"']* <=> match string with: ( if ) any whitespace, ( if ) any alphanumeric characters and ( if ) any characters from list: "-", "=", """, "'" literally
[^'s])? <=> previous string cannot end with one or more whitespaces
('s*)? <=> match ( if ) any whitespace
> <=> match the character: ">" literally
('s*)? <=> match ( if ) any whitespace
(['s'w-='"']* <=> match string with: ( if ) any whitespace, ( if ) any alphanumeric characters and ( if ) any characters from list: "-", "=", """, "'" literally
[^'s]) <=> previous string cannot end with one or more whitespaces
('s*)? <=> match ( if ) any whitespace
< <=> match the character: "<" literally
('s*)? <=> match ( if ) any whitespace
'/ <=> match the character "/" - this character is special in RegExp and you must precede this character by: "'"
('s*)? <=> match ( if ) any whitespace
h <=> match the character: "h" literally
([1-6]) <=> match single character from list: "1", "2", "3", "4", "5", "6" literally
('s*)? <=> match ( if ) any whitespace
> <=> match the character: ">" literally
我的代码没有更正插入代码的语法。
只会标签:"& lt; h1>","& lt; h2>","& lt; h3>","& lt; h4>","& lt; h5>","& lt; h6>及其内容。
示例fiddle: JSFiddle
相关文章:
- 在高图表中,每x步只显示标签
- 如何隐藏和显示标签控件JavaScript
- 如何在angular js的ng应用程序中以普通的htm显示标签
- 使用细丝组tablesaw插件时未显示标签
- 用Javascript在具有相同类的按钮上显示标签
- Dijit 按钮显示图标,但不以编程方式显示标签
- 当圆形图像在圆轮中发生变化时,如何在顶部显示标签
- jqPlot - 无法使 x 轴正确显示标签
- 在堆叠条形图上显示标签
- 在酒窝气泡图中的每个气泡内显示标签
- Struts2 显示标签动态链接显示相同的行值
- HTML,显示标签的输入值
- JS sup() 在 html 中显示标签
- 如何在我的WinJS工具栏上显示标签
- d3图表宽度不会在x轴上显示标签
- 如何在鼠标悬停在条形图上时显示标签
- Select2.js-如何防止显示标签
- 我无法在JQChart中显示标签
- 使用 jQuery 验证并尝试在标签无效时突出显示标签
- 将鼠标悬停在圆上时显示标签