js自动闭合html标签
假如我有一个DIV,如果没有闭合后面的样式都会乱了,这样的代码可能会影响后面的样式,我希望用JS去自动闭合这种没有闭合的标签。
代码如下:
<script type="text/javascript">
// Close HTML Tags
--------------------------------------------
function closeHTML(str){
var
arrTags=["span","font","b","u","i","h1","h2","h3","h4","h5","h6","p","li","ul","table","div"];
for(var i=0;i<arrTags.length;i++){
var intOpen=0;
var intClose=0;
var re=new RegExp("\\<"+arrTags[i]+"( [^\\<\\>]+|)\\>","ig");
var arrMatch=str.match(re);
if(arrMatch!=null) intOpen=arrMatch.length;
re=new RegExp("\\<\\/"+arrTags[i]+"\\>","ig");
arrMatch=str.match(re);
if(arrMatch!=null) intClose=arrMatch.length;
for(var j=0;j<intOpen-intClose;j++){
str+="</"+arrTags[i]+">";
}
/*for(var j=(intOpen-intClose-1);j>=0;j--){
str+="</"+arrTags[i]+">";
}*/
}
return str;
}
</script>
本文地址:http://www.tuquu.com/tutorial/wd2716.html