我有以下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;
答案 0 :(得分:0)
试试这段代码:
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&gt;”,“&lt; h2&gt;”,“&lt; h3&gt;”,“&lt; h4&gt;” ,“&lt; h5&gt;”,“&lt; h6&gt;”和他们的内容。
示例小提琴:JSFiddle