当前位置:编程学习 > C#/ASP.NET >>

JS代码在HTML里正常运行为什么在.net里就不能正常运行呢?

在HTML里的代码:
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> 
<title>新闻菜单</title> 
<style type="text/css"> 
.titleStyle{ 
background-color:#D6F1B4;color:#400040;border-top:1px solid #FFFFFF;font-size:10pt;cursor:hand; 

.contentStyle{ 
background-color:#ffffff;color:#51C745;font-size:10pt; 


a{ 
color:#51C745; 

body{ 
font-size:9pt;
margin:0px;

</style> 
</head> 
<body> 

<script language="JavaScript" type="text/javascript"> 

var layerTop=150; //菜单顶边距 
var layerLeft=20; //菜单左边距 
var layerWidth=200; //菜单总宽 
var titleHeight=30; //标题栏高度 
var contentHeight=300; //内容区高度 
var stepNo=10; //移动步数,数值越大移动越慢 

var itemNo=0;runtimes=0; 
document.write('<span id=itemsLayer style="position:absolute;overflow:hidden;border:1px solid #c0de98;left:'+layerLeft+';top:'+layerTop+';width:'+layerWidth+';">'); 

function addItem(itemTitle,itemContent){ 
itemHTML='<div id=item'+itemNo+' itemIndex='+itemNo+' style="position:relative;left:0;top:'+(-contentHeight*itemNo)+';width:'+layerWidth+';"><table width=100% cellspacing="0" cellpadding="0">'+ 
'<tr><td height='+titleHeight+' onclick=changeItem('+itemNo+') class="titleStyle" align=center>'+itemTitle+'</td></tr>'+ 
'<tr><td height='+contentHeight+' class="contentStyle">'+itemContent+'</td></tr></table></div>'; 
document.write(itemHTML); 
itemNo++;
}
//退出后台
function close() {
    window.close();
}
//添加菜单标题和内容,可任意多项,注意格式: 
addItem('欢迎','<center>欢迎光临</center>'); 
addItem('管理员权限','<center><a href="02_02.aspx" target="number2">物流公司查看和删除</a><BR><BR><a href="02_06.aspx"  target="number2">物流公司添加</a></center>'); 
addItem('商品管理','<center><a href="02_04.aspx"  target="number2">商品输入</a><BR><BR><a href="02_03.aspx"  target="number2">商品出售查询</a></center>'); 
addItem('物流操作','<center><a href="01_05.aspx" target="number2">订单配货</a><BR><BR><a href="01_03.aspx" target="number2">订单发货</a><BR><BR><a href="01_08.aspx" target="number2">客户签收</a>'); 
addItem('物流运单查询','<center><a href="01_01.aspx" target="number2">运单查询</a>');
addItem('注销|退出','<center><a href="" onclick="close()">退出</a><BR><BR><a href="../01_07.aspx" target="_parent">注销</a>');

document.write('</span>') 
document.all.itemsLayer.style.height=itemNo*titleHeight+contentHeight; 

toItemIndex=itemNo-1;onItemIndex=itemNo-1; 

function changeItem(clickItemIndex){ 
toItemIndex=clickItemIndex; 
if(toItemIndex-onItemIndex>0) moveUp(); else moveDown(); 
runtimes++; 
if(runtimes>=stepNo){ 
onItemIndex=toItemIndex; 
runtimes=0;} 
else 
setTimeout("changeItem(toItemIndex)",10); 


function moveUp(){ 
for(i=onItemIndex+1;i<=toItemIndex;i++) 
eval('document.all.item'+i+'.style.top=parseInt(document.all.item'+i+'.style.top)-contentHeight/stepNo;'); 


function moveDown(){ 
for(i=onItemIndex;i>toItemIndex;i--) 
eval('document.all.item'+i+'.style.top=parseInt(document.all.item'+i+'.style.top)+contentHeight/stepNo;'); 

changeItem(0); 

</script> 
</body> 
</html> 
在.net里应该怎么写才能和HTML里一样呢 下面是在.net里的代码<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="RUNJiuZhou.Web.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <%--<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/> --%>
<title>新闻菜单</title> 

<script language="JavaScript" type="text/javascript"> 

var layerTop=150; //菜单顶边距 
var layerLeft=20; //菜单左边距 
var layerWidth=200; //菜单总宽 
var titleHeight=30; //标题栏高度 
var contentHeight=300; //内容区高度 
var stepNo=10; //移动步数,数值越大移动越慢 

var itemNo=0;runtimes=0; 
document.write('<span id=itemsLayer style="position:absolute;overflow:hidden;border:1px solid #c0de98;left:'+layerLeft+'px;top:'+layerTop+'px;width:'+layerWidth+'px;">'); 

function addItem(itemTitle,itemContent){ 
itemHTML='<div id=item'+itemNo+' itemIndex='+itemNo+' style="position:relative;left:0;top:'+(-contentHeight*itemNo)+';width:'+layerWidth+'px;"><table width=100% cellspacing="0" cellpadding="0">'+ 
'<tr><td height='+titleHeight+'px onclick=changeItem('+itemNo+') class="titleStyle" align=center>'+itemTitle+'</td></tr>'+ 
'<tr><td height='+contentHeight+' class="contentStyle">'+itemContent+'</td></tr></table></div>'; 
document.write(itemHTML); 
itemNo++;
}
//退出后台
function close() {
    window.close();
}
//添加菜单标题和内容,可任意多项,注意格式: 
addItem('欢迎','<center>欢迎光临</center>'); 
addItem('管理员权限','<center><a href="02_02.aspx" target="number2">物流公司查看和删除</a><BR><BR><a href="02_06.aspx"  target="number2">物流公司添加</a></center>'); 
addItem('商品管理','<center><a href="02_04.aspx"  target="number2">商品输入</a><BR><BR><a href="02_03.aspx"  target="number2">商品出售查询</a></center>'); 
addItem('物流操作','<center><a href="01_05.aspx" target="number2">订单配货</a><BR><BR><a href="01_03.aspx" target="number2">订单发货</a><BR><BR><a href="01_08.aspx" target="number2">客户签收</a>'); 
addItem('物流运单查询','<center><a href="01_01.aspx" target="number2">运单查询</a>');
addItem('注销|退出','<center><a href="" onclick="close()">退出</a><BR><BR><a href="../01_07.aspx" target="_parent">注销</a>');

document.write('</span>') 
document.all.itemsLayer.style.height=itemNo*titleHeight+contentHeight; 

toItemIndex=itemNo-1;onItemIndex=itemNo-1; 

function changeItem(clickItemIndex){ 
toItemIndex=clickItemIndex; 
if(toItemIndex-onItemIndex>0) moveUp(); else moveDown(); 
runtimes++; 
if(runtimes>=stepNo){ 
onItemIndex=toItemIndex; 
runtimes=0;} 
else 
setTimeout("changeItem(toItemIndex)",10); 


function moveUp(){ 
for(i=onItemIndex+1;i<=toItemIndex;i++) 
eval('document.all.item'+i+'.style.top=parseInt(document.all.item'+i+'.style.top)-contentHeight/stepNo;'); 


function moveDown(){ 
for(i=onItemIndex;i>toItemIndex;i--) 
eval('document.all.item'+i+'.style.top=parseInt(document.all.item'+i+'.style.top)+contentHeight/stepNo;'); 

changeItem(0); 

</script>     
</head>
<body>


</body>

</html>
--------------------编程问答-------------------- JS 抽取到JS文件中

页面引用JS  调用里面方法。 --------------------编程问答-------------------- 没抛异常提示你,form呢

<body>
    <form id="form1" runat="server">
    </form>
</body>
--------------------编程问答-------------------- 你在html里这样的
<body>
<script language="JavaScript" type="text/javascript">
</script>
</body>
但是在.net里,你却故意想当然的改成了
<head>
<script language="JavaScript" type="text/javascript">
</script>
</head>
<body>
</body>

如果页面需要PostBack,则还需要在body里直接嵌套一个
<form id="form1" runat="server">
<script language="JavaScript" type="text/javascript">
</script>
</form>
--------------------编程问答-------------------- --------------------编程问答-------------------- 注意 路径 用相对路径 --------------------编程问答--------------------  js不兼容的问题
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

把这段去了就OK了,或者找个兼容的js
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,