<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD><BODY>
<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 80;  //定义允许图片宽度
var iheight = 80;  //定义允许图片高度
image.src=ImgD.src;
if(image.width>0 && image.height>0&&flag==false){
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){  
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;  
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){  
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;  
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
} function ref(ImgD)
{
flag =!flag;
if(flag)
{
var image=new Image();
var iwidth = 80;  //定义允许图片宽度
var iheight = 80;  //定义允许图片高度
image.src=ImgD.src;
ImgD.width=image.width;  
ImgD.height=image.height;
}
else
{
DrawImage(ImgD)
}
}
//-->
</script><img src="http://community.csdn.net/ADV/Images/csdn120x360.GIF" onload = "DrawImage(this)" onclick="ref(this)">
</BODY>
</HTML>

解决方案 »

  1.   

    谢谢您的帮助我现在的问题是需要进行处理的这部分代码是从数据库中提出来的,用一个变量表示,例如<%=content%>。
    而页面中其它部分的图片不需要调用这个方法进行处理,所以我就需要动态的向content所代表代码中的<img>标签中写入方法调用。
    我想问的是怎样能这样动态的写入
      

  2.   

    asp的方法:<%
    Dim re, strMatches, strMatch, tmpStr1, tmpStr2
    Set re=new RegExp
    re.IgnoreCase =True
    re.Global=True re.Pattern="\[img\](.*?)\[\/img\]"
    Set strMatches=re.Execute(strContent)
    For Each strMatch In strMatches
    tmpStr1=CheckLinkStr(strMatch.SubMatches(0))
    strContent=Replace(strContent,strMatch.Value,"<img src="""&tmpStr1&""" border=""0"" onload=""javascript:DrawImage(this);""  alt=""按此在新窗口打开图片"" onmouseover=""this.style.cursor='hand';"" onclick=""window.open(this.src);"" />")
    Next
    Set strMatches=Nothing re.Pattern="\[img=(left|right|center|absmiddle)\](.*?)\[\/img\]"
    Set strMatches=re.Execute(strContent)
    For Each strMatch In strMatches
    tmpStr1=strMatch.SubMatches(0)
    tmpStr2=CheckLinkStr(strMatch.SubMatches(1))
    strContent=Replace(strContent,strMatch.Value,"<img src="""&tmpStr2&""" align="""&tmpStr1&"""  onload=""javascript:DrawImage(this);""  border=""0"" alt=""按此在新窗口打开图片"" onmouseover=""this.style.cursor='hand';"" onclick=""window.open(this.src);"" />")
    Next
    Set strMatches=Nothing
    %>