JavaScript在form表单里面不能运行
我把页面代码贴出来:myimg-1.1.js 代码:
var MyImg = {
img : [],
imglink : [],
imgalt : [],
num : -1,
time : 5000,
$ : function(id) {
return document.getElementById(id);
},
init : function(img, imglink, imgalt, time) {
this.img = img;
this.imglink = imglink;
this.imgalt = imgalt;
if(time && time != 0 && time != '0') this.time = time;
},
show : function(idx) {
if(document.all) {
myimgRotator.filters.revealTrans.Transition = Math.floor(Math.random() * 20);
myimgRotator.filters.revealTrans.apply();
}
document.images.myimgRotator.src = this.img[idx];
document.images.myimgRotator.alt = this.imgalt[idx];
if(document.all) myimgRotator.filters.revealTrans.play();
for(var i=0;i<this.img.length;i++) {
var o = this.$('myimg_a_' + i);
if(i == idx) {
o.style.backgroundColor = 'red';
o.innerHTML = ' <font color=#ffffff>' + (i+1) + '</font> ';
} else {
o.style.backgroundColor = '';
o.innerHTML = ' ' + (i+1) + ' ';
}
}
this.$('myimgalt').innerHTML = ' <a href="javascript:MyImg.linkURL();" style="text-decoration:none;color:blue;">' + (this.imgalt && this.imgalt[idx] ? this.imgalt[idx] : '') + '</a>';
},
next : function() {
this.num++;
if(this.num >= this.img.length) this.num = 0;
this.show(this.num);
setTimeout("MyImg.next()", this.time);
},
slct : function(idx) {
this.num = idx - 1;
var tmp = this.time;
this.time = 1000000;
this.show(idx);
this.time = tmp;
},
linkURL : function() {
var url = (this.imglink && this.imglink[this.num]) ? this.imglink[this.num] : this.img[this.num];
window.open(url);
},
getHTML : function(img, imglink, imgalt, w, h, time) {
this.init(img, imglink, imgalt, time);
w = (w) ? w : 400;
h = (h) ? h : 300;
var aa = '';
for(var i=0;i<this.img.length;i++) {
aa += '<a id=myimg_a_' + i + ' onclick="MyImg.slct(' + i + ');" style="color:#000;cursor:pointer;" title="' + this.imgalt[i] + '"> ' + (i+1) + ' </a>';
}
return '<table boder=0 width=' + w + ' height=' + h + '><tr><td><a href="javascript:MyImg.linkURL();"><img style="FILTER:revealTrans(duration=2,transition=20,border:1px solid #000000" src="' + this.img[0] + '" width=' + w + ' height=' + h + ' border=0 name="myimgRotator" alt=""></a><div style="font-size:13px;margin-top:-30px;"><table border=0 width=' + w + ' bgcolor="#FFFFFF" style="filter:alpha(opacity=80);opacity:0.8;"><tr><td width=50%><div id=myimgalt></div></td><td width=50% align=right>' + aa + '</td></tr></table></div></td></tr></table>';
},
// ************** 通用调用函数 ******************
disp : function(img, imglink, imgalt, w, h, time, id) {
var html = this.getHTML(img, imglink, imgalt, w, h, time);
if(id) this.$(id).innerHTML = html;
else document.write(html);
this.next();
}
};
aspx页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
var img = [
"imgs/5.jpg",
"imgs/2.jpg",
"imgs/3.jpg",
"imgs/4.jpg",
"imgs/1.jpg",
"imgs/6.jpg",
"imgs/7.jpg",
"imgs/8.jpg"
];
var imglink = [
"http://www.baidu.com",
"http://www.,
"http://www.,
"http://www.,
"http://www.,
"http://www.baidu.com",
"http://www.,
"http://www.
];
var imgalt = [
"百度",
"雅虎",
"QQ",
"新浪",
"中国人民",
"四川",
"这是大哥",
"嘿嘿 "
];
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript" src="myimg-1.1.js"></script>
<script type="text/javascript">MyImg.disp(img, imglink, imgalt, 400, 300, 5000);</script>
</div>
</form>
</body>
</html>
这代码放到HTML页面运行就一切正常,或者把这里的 <form id="form1" runat="server">这个表单去掉也可以运行,不明白为什么,form表单去掉了其他内容就不晓得改咋写了呢。请高手解答。