js自動閉合html標(biāo)簽
假如我有一個DIV,如果沒有閉合后面的樣式都會亂了,這樣的代碼可能會影響后面的樣式,我希望用JS去自動閉合這種沒有閉合的標(biāo)簽。
代碼如下:
<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://irelandcustomcontracting.com/tutorial/wd2716.html