javascript问题,看看代码错哪了(评分控件)
<head><title></title><scripttype="text/javascript">functionMouseIn(){vartablescore=do...
<head>
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.lenth; i++) {
tds[i].onmouseover = tdOnMouseOver(tds, i);
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(tds, loc) {
for(var i = 0; i <= loc; i++) {
tds[i].innertext = "★";
}
for (i = loc; i < tds.lenth; i++) {
tds[i].innertext = "☆";
}
}
</script>
</head>
<body onload="MouseIn()">
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
</body>
</html>
谁能说说我代码的错误点在哪?
如果能讲明白的话,可以再加10分 展开
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.lenth; i++) {
tds[i].onmouseover = tdOnMouseOver(tds, i);
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(tds, loc) {
for(var i = 0; i <= loc; i++) {
tds[i].innertext = "★";
}
for (i = loc; i < tds.lenth; i++) {
tds[i].innertext = "☆";
}
}
</script>
</head>
<body onload="MouseIn()">
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
</body>
</html>
谁能说说我代码的错误点在哪?
如果能讲明白的话,可以再加10分 展开
13个回答
展开全部
1, 拼写错误 lenth -> length, innertext -> innerText;
2, 语义错误 tds[i].onmouseover = tdOnMouseOver(tds, i); 这样代码的意思是将tdOnMouseOver(tds, i)的返回值赋给tds[i].onmouseover, 应当使用function字面量, function(){tdOnMouseOver(tds, i);}
3, 对运行机制理解的错误 function(){tdOnMouseOver(tds, i);}, 定义了一个函数字面量, 代码tdOnMouseOver(tds, i);的运行时间是在事件发生时, 这个时候MouseIn方法已经执行结束了, i的值为始终为5, 解决的办法是将各自的编号写入html元素中: tds[i].title=i;这样.
调用的时候可以在事件方法中使用this.title引用. 所以就不需要编号参数了function()tds[i].onmouseover = {tdOnMouseOver(tds);};
4, 一些建议: 写javascript一定要规范缩进, 否则在上千行规模的js中错删一个花括号什么的, 找死人. 还有引号的问题, js代码中建议使用单引号, 拼接html时给标签属性使用双引号, 这样就不需要使用转义符, 就不太容易出错.
楼上几位写的应该都能用, 不过没有考虑浏览器兼容性问题, 给你个各种浏览器通用优化版本
Trident核心(IE, Maxthon), Gecko核心(FireFox), Webkit核心(Safari, Google)都是可以用的
<head>
<title></title>
<script type="text/javascript">
var stars = 10; //随便几星
function createStars() {
//拼接html, 将编号固化到html中, 就不需要再另外处理编号了
var html = '<table><tr>';
for (var i = 0; i < stars; i++) {
html += '<td id="star-' + i + '" style="cursor: pointer;" onmouseover="javascript: tdOnMouseOver(' + i + ');">☆</td>';
//相当于手工写html代码
//第一个<td id="star-0" style="cursor: pointer;" onmouseover="javascript: tdOnMouseOver(0);">☆</td>
//第二个<td id="star-1" style="cursor: pointer;" onmouseover="javascript: tdOnMouseOver(1);">☆</td>
}
html += '</tr></table>';
document.getElementById('stars').innerHTML = html;
}
function tdOnMouseOver(loc) {
var _td;
for (var i = 0; i < stars; i++) {
_td = document.getElementById('star-' + i);
_td.innerText = _td.textContent = i <= loc ? '★' : '☆';
}
}
</script>
</head>
<body onload="createStars()">
<div id="stars"></div>
</body>
</html>
2, 语义错误 tds[i].onmouseover = tdOnMouseOver(tds, i); 这样代码的意思是将tdOnMouseOver(tds, i)的返回值赋给tds[i].onmouseover, 应当使用function字面量, function(){tdOnMouseOver(tds, i);}
3, 对运行机制理解的错误 function(){tdOnMouseOver(tds, i);}, 定义了一个函数字面量, 代码tdOnMouseOver(tds, i);的运行时间是在事件发生时, 这个时候MouseIn方法已经执行结束了, i的值为始终为5, 解决的办法是将各自的编号写入html元素中: tds[i].title=i;这样.
调用的时候可以在事件方法中使用this.title引用. 所以就不需要编号参数了function()tds[i].onmouseover = {tdOnMouseOver(tds);};
4, 一些建议: 写javascript一定要规范缩进, 否则在上千行规模的js中错删一个花括号什么的, 找死人. 还有引号的问题, js代码中建议使用单引号, 拼接html时给标签属性使用双引号, 这样就不需要使用转义符, 就不太容易出错.
楼上几位写的应该都能用, 不过没有考虑浏览器兼容性问题, 给你个各种浏览器通用优化版本
Trident核心(IE, Maxthon), Gecko核心(FireFox), Webkit核心(Safari, Google)都是可以用的
<head>
<title></title>
<script type="text/javascript">
var stars = 10; //随便几星
function createStars() {
//拼接html, 将编号固化到html中, 就不需要再另外处理编号了
var html = '<table><tr>';
for (var i = 0; i < stars; i++) {
html += '<td id="star-' + i + '" style="cursor: pointer;" onmouseover="javascript: tdOnMouseOver(' + i + ');">☆</td>';
//相当于手工写html代码
//第一个<td id="star-0" style="cursor: pointer;" onmouseover="javascript: tdOnMouseOver(0);">☆</td>
//第二个<td id="star-1" style="cursor: pointer;" onmouseover="javascript: tdOnMouseOver(1);">☆</td>
}
html += '</tr></table>';
document.getElementById('stars').innerHTML = html;
}
function tdOnMouseOver(loc) {
var _td;
for (var i = 0; i < stars; i++) {
_td = document.getElementById('star-' + i);
_td.innerText = _td.textContent = i <= loc ? '★' : '☆';
}
}
</script>
</head>
<body onload="createStars()">
<div id="stars"></div>
</body>
</html>
追问
大侠,能留个QQ么?
我多给你点分。
追答
不用QQ, 早就看穿了那些崽子们的丑恶面目, 果断撤离
分多少无所谓, 反正我也不用, 你自己省着点
有事hi
展开全部
<head>
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].title=i;
tds[i].onmouseover = function(){tdOnMouseOver(this.title);}
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(loc) {
document.title=loc;
var tds=document.getElementById("tablescore").getElementsByTagName("td");
var i;
for(i = 0; i <= loc; i++) {
tds[i].innerHTML = "★";
}
for (; i < tds.length; i++) {
tds[i].innerHTML = "☆";
}
}
</script>
</head>
<body>
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
<script>MouseIn();</script>
</body>
</html>
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].title=i;
tds[i].onmouseover = function(){tdOnMouseOver(this.title);}
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(loc) {
document.title=loc;
var tds=document.getElementById("tablescore").getElementsByTagName("td");
var i;
for(i = 0; i <= loc; i++) {
tds[i].innerHTML = "★";
}
for (; i < tds.length; i++) {
tds[i].innerHTML = "☆";
}
}
</script>
</head>
<body>
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
<script>MouseIn();</script>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<html>
<body onload="MouseIn()">
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
</body>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].onmouseover = function(){tdOnMouseOver(tds);}
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(tds) {
for (i = 0; i < tds.length; i++) {
tds[i].innerHTML = "☆";
}
for(var i = 0; i < tds.length; i++) {
tds[i].innerHTML = "★";
if(tds[i]==event.srcElement) break;
}
}
</script>
</html>
<body onload="MouseIn()">
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
</body>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].onmouseover = function(){tdOnMouseOver(tds);}
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(tds) {
for (i = 0; i < tds.length; i++) {
tds[i].innerHTML = "☆";
}
for(var i = 0; i < tds.length; i++) {
tds[i].innerHTML = "★";
if(tds[i]==event.srcElement) break;
}
}
</script>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
改了下,你自己对照下哈
<head>
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].title=i;
tds[i].onmouseover = function(){tdOnMouseOver(this.title);}
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(loc) {
document.title=loc;
var tds=document.getElementById("tablescore").getElementsByTagName("td");
var i;
for(i = 0; i <= loc; i++) {
tds[i].innerHTML = "★";
}
for (; i < tds.length; i++) {
tds[i].innerHTML = "☆";
}
}
</script>
</head>
<body>
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
<script>MouseIn();</script>
</body>
</html>
<head>
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].title=i;
tds[i].onmouseover = function(){tdOnMouseOver(this.title);}
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(loc) {
document.title=loc;
var tds=document.getElementById("tablescore").getElementsByTagName("td");
var i;
for(i = 0; i <= loc; i++) {
tds[i].innerHTML = "★";
}
for (; i < tds.length; i++) {
tds[i].innerHTML = "☆";
}
}
</script>
</head>
<body>
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
<script>MouseIn();</script>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<html>
<head>
<title>评分模拟</title>
<script type="text/javascript">
var current;
function mouseIn(){
//得到id为score的table节点
var tableNode = document.getElementById("score");
//得到该table节点下面所有的td节点
var tds = tableNode.getElementsByTagName("td");
//定义变量得到tds数组的长度
var len = tds.length;
//定义一个变量标示当前的节点
var cur = 0;
//为所有的td节点绑定onmouseover事件
for(var i=0;i<len;i++){
tds[i].index = i;
tds[i].onmouseover = function(){over(this.index);}
tds[i].onmouseout = function(){out(this.index)}
tds[i].style.cursor = "pointer";
}
}
function over(cur){
var tds = document.getElementById("score").getElementsByTagName("td");
for(var j=0;j<=cur;j++){
tds[j].innerHTML = "★";
}
}
function out(cur){
if(current>=cur){
var tds = document.getElementById("score").getElementsByTagName("td");
for(var j =cur;j<tds.length;j++){
tds[j].innerHTML = "☆";
}
}
current = cur;
}
</script>
</head>
<body onload="mouseIn()">
<table id="score">
<tr>
<td>☆</td>
<td>☆</td>
<td>☆</td>
<td>☆</td>
<td>☆</td>
</tr>
</table>
</body>
</html>
经过修改,可以一个星星都不选择,也就是评分的时候可以选择评0分
<head>
<title>评分模拟</title>
<script type="text/javascript">
var current;
function mouseIn(){
//得到id为score的table节点
var tableNode = document.getElementById("score");
//得到该table节点下面所有的td节点
var tds = tableNode.getElementsByTagName("td");
//定义变量得到tds数组的长度
var len = tds.length;
//定义一个变量标示当前的节点
var cur = 0;
//为所有的td节点绑定onmouseover事件
for(var i=0;i<len;i++){
tds[i].index = i;
tds[i].onmouseover = function(){over(this.index);}
tds[i].onmouseout = function(){out(this.index)}
tds[i].style.cursor = "pointer";
}
}
function over(cur){
var tds = document.getElementById("score").getElementsByTagName("td");
for(var j=0;j<=cur;j++){
tds[j].innerHTML = "★";
}
}
function out(cur){
if(current>=cur){
var tds = document.getElementById("score").getElementsByTagName("td");
for(var j =cur;j<tds.length;j++){
tds[j].innerHTML = "☆";
}
}
current = cur;
}
</script>
</head>
<body onload="mouseIn()">
<table id="score">
<tr>
<td>☆</td>
<td>☆</td>
<td>☆</td>
<td>☆</td>
<td>☆</td>
</tr>
</table>
</body>
</html>
经过修改,可以一个星星都不选择,也就是评分的时候可以选择评0分
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
群主来也,看我的吧,哈
<head>
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].title=i;
tds[i].onmousemove = function (){tdOnMouseOver(this.title)};
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(loc) {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (i = 0; i < tds.length; i++) {
tds[i].innerText = "☆";
}
for(var i = 0; i <= loc; i++) {
tds[i].innerText = "★";
}
}
</script>
</head>
<body onLoad="MouseIn()">
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
</body>
</html>
<head>
<title></title>
<script type="text/javascript">
function MouseIn() {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].title=i;
tds[i].onmousemove = function (){tdOnMouseOver(this.title)};
tds[i].style.cursor = "pointer";
}
}
function tdOnMouseOver(loc) {
var tablescore = document.getElementById("tablescore");
var tds = tablescore.getElementsByTagName("td");
for (i = 0; i < tds.length; i++) {
tds[i].innerText = "☆";
}
for(var i = 0; i <= loc; i++) {
tds[i].innerText = "★";
}
}
</script>
</head>
<body onLoad="MouseIn()">
<table id="tablescore"><tr><td>☆</td><td>☆</td><td>☆</td><td>☆</td><td>☆</td></tr></table>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询