元素的ID慎用length
先来看看一段错误的代码:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <mce:script type="text/javascript"><!-- (function(){ //id=length var d=document.getElementsByTagName("input").length; alert(d); })(); // --></mce:script> </head> <body> <form > <input type="text" /> <input type="submit" /> </form> </body> </html>
这段代码中第一个input的ID为length,我们可以在Ie,firefox的结果都是0,这些都是<input type="text" />惹的祸,因此我们可要注意了
最后更新:2017-04-02 00:06:42