处理HTML代码的若干函数

时间:2009-08-21 13:21:00 

经过了上个星期的努力学习,对处理html又有了新的发现

感觉真的很不错

可以说js的威力在处理html代码方面我又有所领悟了

1、截取特定长度字符串


    Public Shared Function CutString1(ByVal str As String, ByVal length As Integer) As String


        Dim i As Integer = 0
        Dim j As Integer = 0
        For Each chr As Char In str
            If Asc(chr) > 127 Then
                i = i + 2
            Else
                i += 1
                If (i > length) Then
                    str = str.Substring(0, j) + ""
                    Exit For
                End If
            End If
            j += 1
        Next
        Return str

    End Function

2、在在textarea(textbox里面的textmode为dul),对其输入的字符转换成html格式

Public Function HtmlEncode()(ByVal str As String) As String
        Dim L As Integer
        Dim result As String
        If str = "" Then
            Return ""
        End If
        L = Len(str)
        Dim i As Integer
        For i = 1 To L
            Select Case Mid(str, i, 1)
                Case Chr(13)
                    result = result + "<br>"
                Case Chr(34)
                    result = result + "&quot;"
                Case Chr(32)
                    'result=result+"&nbsp;"
                    If i + 1 <= L And i - 1 > 0 Then
                        If Mid(str, i + 1, 1) = Chr(32) Or Mid(str, i + 1, 1) = Chr(9) Or Mid(str, i - 1, 1) = Chr(32) Or Mid(str, i - 1, 1) = Chr(9) Then
                            result = result + "&nbsp;"
                        Else
                            result = result + " "
                        End If
                    Else
                        result = result + "&nbsp;"
                    End If
                Case Chr(9)
                    result = result + "    "
                Case Else
                    result = result + Mid(str, i, 1)
            End Select
        Next
        Return result
    End Function

3、在textarea(textbox里面的textmode为dul),对其输入的字符转换成html格式输出

 Public Shared Function StripHTML()
Function StripHTML(ByVal strHtml As String) As String
        Dim aryReg() As String = New String() {"<script[^>]*?>.*?</script>", "<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]ws[^\7])*?\7ws\w+)ws.{0})ws\s)*?(\/\s*)?>", "([\r\n])[\s]+", "&(quotws#34);", "&(ampws#38);", "&(ltws#60);", "&(gtws#62);", "&(nbspws#160);", "&(iexclws#161);", "&(centws#162);", "&(poundws#163);", "&(copyws#169);", "&#(\d+);", "-->", "<!--.*\n"}

        Dim aryRep() As String = {"", "", "", """", "&", "", "", " ", "\xa1", Chr(161), "\xa2", Chr(162), "\xa3", Chr(163), "\xa9", Chr(169), "", "\r\n", ""}


        Dim NewReg As String = aryReg(0)
        Dim strOutput As String = strHtml
        Dim i As Integer
        For i = 0 To aryReg.Length - 1 Step i + 1
            Dim regex As Regex = New Regex(aryReg(i), RegexOptions.IgnoreCase)
            strOutput = regex.Replace(strOutput, aryRep(i))
        Next

        strOutput.Replace("<", "")
        strOutput.Replace(">", "")
        strOutput.Replace("\r\n", "")


        Return strOutput
    End Function

4、一个plus js代码,作用是把所有的tooltip格式化后转变一个层,特定输出
var pltsPop=null;
var pltsoffsetX = 10;
var pltsoffsetY = 15;
var pltsPopbg="#ffff00";
var pltsPopfg="#111111";
var pltsTitle="";
document.write('<div id=pltsTipLayer style="display: none;position: absolute; z-index:10001"></div>');
function pltsinits()
{
    document.onmouseover   = plts;
    document.onmousemove = moveToMouseLoc;
}
function plts()
{  var o=event.srcElement;
    if(o.alt!=null && o.alt!=""){o.dypop=o.alt;o.alt=""};
    if(o.title!=null && o.title!=""){o.dypop=o.title;o.title=""};
    pltsPop=o.dypop;
    if(pltsPop!=null&&pltsPop!=""&&typeof(pltsPop)!="undefined")
    {
pltsTipLayer.style.left=-1000;
pltsTipLayer.style.display='';
var Msg=pltsPop.replace(/\n/g,"<br>");
Msg=Msg.replace(/\0x13/g,"<br>");
var re=/\{(.[^\{]*)\}/ig;
if(!re.test(Msg))pltsTitle="[MyTopWin.Com] 拓文CMS ASP.net系统";
else{
   re=/\{(.[^\{]*)\}(.*)/ig;
     pltsTitle=Msg.replace(re,"$1")+"&nbsp;";
   re=/\{(.[^\{]*)\}/ig;
   Msg=Msg.replace(re,"");
   Msg=Msg.replace("<br>","");}
  
        var content =
       '<table style="FILTER:alpha(opacity=90) shadow(color=#bbbbbb,direction=150);" id=toolTipTalbe border=0><tr><td width="100%"><table class=selet_bg cellspacing="0" cellpadding="0" style="width:100%" style="font size:11px;">'+
       '<tr id=pltsPoptop><th height=12 valign=bottom class=header><p id=topleft align=left>↖'+pltsTitle+'</p><p id=topright align=right style="display:none">'+pltsTitle+'↗</font></th></tr>'+
       '<tr><td style="padding-left:10px;padding-right:10px;padding-top: 4px;padding-bottom:4px;line-height:135%">'+Msg+'</td></tr>'+
       '<tr id=pltsPopbot style="display:none"><th height=12 valign=bottom class=header><p id=botleft align=left>↙'+pltsTitle+'</p><p id=botright align=right style="display:none">'+pltsTitle+'↘</font></th></tr>'+
       '</table></td></tr></table>';
        pltsTipLayer.innerHTML=content;
        toolTipTalbe.style.width=Math.min(pltsTipLayer.clientWidth,document.body.clientWidth/2.2);
        moveToMouseLoc();
        return true;
       }
    else
    {
     pltsTipLayer.innerHTML='';
       pltsTipLayer.style.display='none';
        return true;
    }
}

function moveToMouseLoc()
{
if(pltsTipLayer.innerHTML=='')return true;
var MouseX=event.x;
var MouseY=event.y;
//window.status=event.y;
var popHeight=pltsTipLayer.clientHeight;
var popWidth=pltsTipLayer.clientWidth;
if(MouseY+pltsoffsetY+popHeight>document.body.clientHeight)
{
    popTopAdjust=-popHeight-pltsoffsetY*1.5;
    pltsPoptop.style.display="none";
    pltsPopbot.style.display="";
}
  else
{
     popTopAdjust=0;
    pltsPoptop.style.display="";
    pltsPopbot.style.display="none";
}
if(MouseX+pltsoffsetX+popWidth>document.body.clientWidth)
{
  popLeftAdjust=-popWidth-pltsoffsetX*2;
  topleft.style.display="none";
  botleft.style.display="none";
  topright.style.display="";
  botright.style.display="";
}
else
{
  popLeftAdjust=0;
  topleft.style.display="";
  botleft.style.display="";
  topright.style.display="none";
  botright.style.display="none";
}
pltsTipLayer.style.left=MouseX+pltsoffsetX+document.body.scrollLeft+popLeftAdjust;
pltsTipLayer.style.top=MouseY+pltsoffsetY+document.body.scrollTop+popTopAdjust;
   return true;
}
pltsinits();

标签:函数,html,代码
0
投稿

猜你喜欢

  • Python打包后的exe还原成.py的实现步骤

    2022-08-10 21:57:11
  • Python中图像算术运算的示例详解

    2023-01-01 19:56:43
  • Docker + Nodejs + Kafka + Redis + MySQL搭建简单秒杀环境

    2024-01-21 23:22:38
  • js断点调试经验分享

    2023-08-15 06:19:09
  • Python使用微信itchat接口实现查看自己微信的信息功能详解

    2021-07-29 16:07:20
  • 基于JS设计12306登录页面

    2024-04-29 13:45:12
  • 讲解SQL Server 2005数据库的同义词Bug

    2008-11-28 14:22:00
  • Spring Boot中使用Spring-data-jpa实现数据库增删查改

    2024-01-28 09:41:28
  • Linux ORCLE数据库增量备份脚本

    2009-11-21 09:43:00
  • javascript实现延时显示提示框效果

    2024-04-25 13:10:42
  • 慎用UL列表

    2009-03-25 20:21:00
  • PHP常用字符串函数小结(推荐)

    2023-06-14 00:18:50
  • Python的信号库Blinker用法详解

    2021-06-23 23:23:19
  • 浅析BootStrap模态框的使用(经典)

    2023-08-20 02:21:19
  • Python利用matplotlib绘制折线图的新手教程

    2021-08-18 18:00:01
  • python绘制散点图和折线图的方法

    2021-08-24 04:27:56
  • python简单实现基于SSL的IRC bot实例

    2022-05-29 21:18:03
  • 对python numpy数组中冒号的使用方法详解

    2022-05-15 11:26:41
  • Go语言通过smtp发送邮件的方法

    2024-01-29 19:21:27
  • MySQL中Like概念及用法讲解

    2024-01-21 01:12:07
  • asp之家 网络编程 m.aspxhome.com