日期:2014-05-16  浏览次数:20360 次

利用IMG标签属性重写HTML代码并替换为APPLET代码?
网页中的代码希望是这样:
<img   src= 'HTTP://MYURL/MYFILE.FIL '   width= "400 "   height= "300 "   name= "MYNAME ">

通过JAVASCRIPT代码,使之实现的效果如下面的APPLET代码:
(其中需要传递的属性有:路径,高度,宽度,名称)

<applet   name= "MYNAME "   archive= "MYARCHIVE.JAR "   code= "MYCODEPATH "   width= "400 "   height= "300 ">
<param   name= "file "   value= "HTTP://MYURL/MYFILE.FIL ">
<param   name= "auto "   value= "80 ">
你需要安装JAVA虚拟机才能看APPLET
</applet>

本人不会JS的正则表达式,请会的朋友帮帮忙了!谢谢

------解决方案--------------------
<script>
var str = '\ ' <img src=\ 'HTTP://MYURL/MYFILE.FIL\ ' width= "400 " height= "300 " name= "MYNAME "> ';
var re=/\ ' <img src=\ '(.*?)\ ' width=\ "(.*?)\ " height=\ "(.*?)\ " name=\ "(.*?)\ "> /g;
re.exec(str);
str=str.replace(re, " <applet name=\ "$4\ " archive=\ "MYARCHIVE.JAR\ " code=\ "MYCODEPATH\ " width=\ "$2\ " height=\ "$3\ "> "+ "\n "+ " <param name=\ "file\ " value=\ "$1\ "> "+ "\n "+ " <param name=\ "auto\ " value=\ "80\ "> "+ "\n "+ "你需要安装JAVA虚拟机才能看APPLET "+ "\n "+ " </applet> ");
alert(str)
</script>
------解决方案--------------------
<div id= "myApplet "> <img src= "xxx//MYURL/MYFILE.FIL " width= "400 " height= "300 " name= "MYNAME "> </div>
<script>
window.onload=function()
{
var reg= " ";
var myApplet=document.getElementById( "myApplet ");
var str=myApplet.innerHTML;
if(navigator.appName== "Microsoft Internet Explorer ") //IE
{
reg=/ <IMG height=(\d+) src= "(.*?) " width=(\d+) name=(.*?)> /i;
alert(reg.test(str));
str=str.replace(reg, ' <applet name= "$4 " archive= "MYARCHIVE.JAR " code= "MYCODEPATH " width= "$3 " height= "$1 "> <param name= "file " value= "$2 "> <param name= "auto " value= "80 "> 你需要安装JAVA虚拟机才能看APPLET </applet> ');
}
if(navigator.appName== "Netscape ") //Netscape,Firefox
{
reg=/ <img src= "(.*?) " name= "(.*?) " height= "(\d+) " width= "(\d+) "> /i;
alert(reg.test(str));
str=str.replace(reg, ' <applet name= "$2 " archive= "MYARCHIVE.JAR " code= "MYCODEPATH " width= "$4 " height= "$3 "> <param name= "file " value= "$1 "> <param name= "auto " value= "80 "> 你需要安装JAVA虚拟机才能看APPLET </applet> ');
}
//如果还要兼容其他浏览器,照此继续判断
myApplet.innerHTML=str;
}
</script>