为了账号安全,请及时绑定邮箱和手机立即绑定

为啥我的两个方法必须写在 body 里才能运行,而写在 head里就无法运行呢?

为啥我的两个方法必须写在 body 里才能运行,而写在 head里就无法运行呢?

qq_庞四_0 2016-09-20 00:38:37
<!DOCTYPE html> <html>  <head>   <title> new document </title>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>      <script type="text/javascript">           window.onload = function(){                         // 鼠标移动改变背景,可以通过给每行绑定鼠标移上事件和鼠标移除事件来改变所在行背景色。         var trs=document.getElementsByTagName("tr");         console.log(trs);         for(var i=0;i<trs.length;i++){                  console.log(i);          trs[i].setAttribute("onmouseover","msover(this)");          trs[i].setAttribute("onmouseout","msout(this)");         }                    var as=document.getElementsByTagName("a");                 for(var i=0;i<as.length;i++){                  if (as[i].innerHTML="删除"){                          as[i].setAttribute("onclick","this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode)");                  }                 }                            var newlines = document.getElementsByTagName("input");           for(var i=0;i<newlines.length;i++){                  if (newlines[i].value="添加一行"){                  newlines[i].setAttribute("onclick","addline()")                  }               }      }                     function addline(){                  var newl = document.createElement("tr");                  newl.appendChild(document.createElement("td"));                  newl.appendChild(document.createElement("td"));                  var tda = newl.appendChild(document.createElement("td"));                  newa= document.createElement("a");                  newa.href="javascript:void(0);";                  newa.innerHTML="delrow";                  newa.setAttribute("onclick","this.parentNode.parentNode.remove();");                  tda.appendChild(newa);                  document.getElementsByTagName("tbody")[0].appendChild(newl);                  }       // 编写一个函数,供添加按钮调用,动态在表格的最后一行添加子节点;                         // 创建删除函数   </script>      </head>   <body>     <table border="1" width="50%" id="table">    <tr> <th>学号</th> <th>姓名</th> <th>操作</th>    </tr>      <tr> <td>xh001</td> <td>王小明</td> <td><a href="javascript:;" >删除</a></td>   <!--在删除按钮上添加点击事件  -->    </tr>    <tr> <td>xh002</td> <td>刘小芳</td> <td><a href="javascript:;" >删除</a></td>   <!--在删除按钮上添加点击事件  -->    </tr>      </table>    <input type="button" value="添加一行"  />   <!--在添加按钮上添加点击事件  -->       <script type="text/javascript">         function msover(x){       x.style.backgroundColor = "#ffffff";      }      function msout(x){       x.style.backgroundColor="#F2F2F2";      }      </script>           </body>     </html>为啥我这里的 msover() msout() 方法必须写在 body 里才能运行,而写在 head里的window.onload里就无法运行呢?
查看完整描述

2 回答

?
stone310

TA贡献361条经验 获得超191个赞

关于作用域的问题,只要msover()和msout()放在全局作用域下,就可以执行,分析如下:

trs[i].setAttribute("onmouseover","msover(this)");当这个语句执行,相当于在HTML样式里直接添加事件;HTML下的<tr>就会变成这样  <tr onmouseover="msover(this)">;

当你的msover()方法放在全局变量中,<tr onmouseover="msover(this)">是能成功读取到msover()这个方法的,但是如果你放在局部作用域下;根据JS规则,外部是无法访问局部作用域下的方法,因此就无法识别msover();

放在window.onload=function(){}里也就相当于放在一个局部作用域中,外界就无法访问,所以写在window.onload内无法执行

查看完整回答
反对 回复 2016-09-20
?
qq_猫须_0

TA贡献6条经验 获得超1个赞

应该是从上往下执行,使用方法在上面,执行了,但是没有div,但是div之类还在下面,有div但是没方法,你加一个全部载入后再执行的代码就好了

查看完整回答
反对 回复 2016-09-20
  • 2 回答
  • 0 关注
  • 1522 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信