过滤一个html页面,所以只有某些标签显示

Filter a html page so only certain tags are showing

本文关键字:显示 标签 一个 页面 html 过滤      更新时间:2023-09-26

我有以下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