js 为什么for语句的count永远是一个数

puhjack 2011-12-13 08:59:47
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-type" content = "text/html; charset = gb2312">
<title>date selector</title>
<style type = "text/css" charset = "gb2312">
<!--
.change
{
background-color: red;
}
table
{
width: 183px;
height: 10px;
}
table tr
{
height: 10px;
font-size: 13px;
color: blue;
text-align: center;
}
.arrowSize
{
font-size: 7px;
}
.dateSize
{
font-size: 12px;
}
-->
</style>
</head>
<body>
<table>
<tr class = "arrowSize">
<td><<</td>
<td><</td>
<td class = "dateSize">2011年12月31日</td>
<td>></td>
<td>>></td>
</tr>
</table>
<table>
<tr height = "20">
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
<script>
<!--
var print = "";
var count = 0;
var columnArr = new Array();
for( var iRow = 0; iRow < 6; iRow++ )
{
print += "<tr>";
for( var iCol = 0; iCol < 7; iCol++ )
{
count++;
if( count < 32 )
{
print += "<td onmouseover=this.className='change' onmouseout=this.className='' onclick = \"alert( count );\">"
// 为什么这里的count输出永远是一个数 +count+"</td>";
}
}
print += "</tr>";
}
document.write( print );

var year = 2011;
var month = 12;
var lastDay = new Date(year, month, 0);
lastDay = lastDay.getDate();
var firstDay = new Date(year, month-1, 1);
firstDay = firstDay.getDay();
-->
</script>
</table>
</body>
</html>

代码中红色字体部分就是问题了,为什么count总是同一个数字(42)。
我要得到当前的数怎么修改这个代码?比如 1,2,3,4....
...全文
163 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
p2227 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 puhjack 的回复:]

引用 2 楼 p2227 的回复:
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-type" content = "text/……
[/Quote]这里跟闭包应该不是同一个概念,因为你的代码都输出到页面了,js都执行完毕了。参考
http://topic.csdn.net/u/20111126/20/90bd0aa6-9606-498d-9fa2-6023b9512b22.html
puhjack 2011-12-13
  • 打赏
  • 举报
回复
我看到很多有关的方法都是用闭包做,为什么我这个直接就能得到这个结果啊?
puhjack 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 p2227 的回复:]
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-type" content = "text/html; charset = g……
[/Quote]

难道我就只是
onclick = \"alert( count );\">"
onclick = \"alert( " + count + " );\">"
这样错了吗?
那请问下闭包应该怎么做啊?
puhjack 2011-12-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qianjin036a 的回复:]
JScript code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-type" content = "text/html; charset ……
[/Quote]
可以用闭包做吗?
p2227 2011-12-13
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-type" content = "text/html; charset = gb2312">
<title>date selector</title>
<style type = "text/css" charset = "gb2312">
<!--
.change
{
background-color: red;
}
table
{
width: 183px;
height: 10px;
}
table tr
{
height: 10px;
font-size: 13px;
color: blue;
text-align: center;
}
.arrowSize
{
font-size: 7px;
}
.dateSize
{
font-size: 12px;
}
-->
</style>
</head>
<body>
<table>
<tr class = "arrowSize">
<td><<</td>
<td><</td>
<td class = "dateSize">2011年12月31日</td>
<td>></td>
<td>>></td>
</tr>
</table>
<table>
<tr height = "20">
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
<script>
<!--
var print = "";
var count = 0;
var columnArr = new Array();
for (var iRow = 0; iRow < 6; iRow++) {
print += "<tr>";
for (var iCol = 0; iCol < 7; iCol++) {
count++;
if (count < 32) {
print += "<td onmouseover=this.className='change' onmouseout=this.className='' onclick = \"alert( " + count + " );\">"
// 为什么这里的count输出永远是一个数 +count+"</td>";
}
}
print += "</tr>";
}
document.write(print);

var year = 2011;
var month = 12;
var lastDay = new Date(year, month, 0);
lastDay = lastDay.getDate();
var firstDay = new Date(year, month - 1, 1);
firstDay = firstDay.getDay();
-->
</script>
</table>
</body>
</html>
-晴天 2011-12-13
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv = "Content-type" content = "text/html; charset = gb2312">
<title>date selector</title>
<style type = "text/css" charset = "gb2312">
<!--
.change
{
background-color: red;
}
table
{
width: 183px;
height: 10px;
}
table tr
{
height: 10px;
font-size: 13px;
color: blue;
text-align: center;
}
.arrowSize
{
font-size: 7px;
}
.dateSize
{
font-size: 12px;
}
-->
</style>
</head>
<body>
<table>
<tr class = "arrowSize">
<td><<</td>
<td><</td>
<td class = "dateSize">2011年12月31日</td>
<td>></td>
<td>>></td>
</tr>
</table>
<table>
<tr height = "20">
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
<script>
<!--
var print = "";
var count = 0;
var columnArr = new Array();
for( var iRow = 0; iRow < 6; iRow++ )
{
print += "<tr>";
for( var iCol = 0; iCol < 7; iCol++ )
{
count++;
if( count < 32 )
{
print += "<td onmouseover=this.className='change' onmouseout=this.className='' onclick = \"alert( "+count+" );\">"
// 为什么这里的count输出永远是一个数 +count+"</td>";
}
}
print += "</tr>";
}
document.write( print );

var year = 2011;
var month = 12;
var lastDay = new Date(year, month, 0);
lastDay = lastDay.getDate();
var firstDay = new Date(year, month-1, 1);
firstDay = firstDay.getDay();
-->
</script>
</table>
</body>
</html>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧