关于document对象取值问题:<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Keywords" content="简单的XHTML页面" />
<meta name="Description" content="这是一个简单的XHTML页面" />
<title>简单的XHTML页面</title>
<script>
//键入时自动清空特殊字符
function clearcorps(fm,ipt)   
{ alert(fm+ipt);
标记1:document.fm.ipt.value=document.fm.ipt.value..replace(/[\~\`\%\!\@\#\$\^\&\*\(\)]|[\+\=\|\\\;\:\'\<\>\,\.\/\"\?]/g,'');//通过传值不可用!说document.fm is undefined!
标记2://document.Login.username.value=document.Login.username.value.replace(/[\~\`\%\!\@\#\$\^\&\*\(\)]|[\+\=\|\\\;\:\'\<\>\,\.\/\"\?]/g,'');//当我直接用对象名字时可用!
}
</script>
</head>
<body>
<form id="Login" name="Login">
       <input type="text" class="sbox" value="" name="username" id="username" size="30" onKeyup="clearcorps('Login','username')"/>
</form>
</body>
</html>
大家可以把这代码直接拷贝另存为html文件 打开!我用firefox 提示是document.fm is undefined!
但是明明传过来的fm,ipt分别都是Login,username 啊!为什么会出现这样的情况呢?当我们把标记一的注释掉 把标记2的注释去掉!确实我想要的结果!

解决方案 »

  1.   

    服了U
    你就不会懒一点啊,去掉 document.    。fm 是个参数又不是页面元素   
             
                 
               
    --------------完美的签名线-----------------
    网页游戏开发第一站 http://dev.web863.com
      

  2.   

    document.fm.ipt.value
    ==>>
    document.getElementById("ipt")
    Or
    document.getElementById("fm").getElementById("ipt")
      

  3.   

    原因很简单,
    你是这样调用的:clearcorps('Login','username');
    所以函数体中document.fm 就是document.'Login' 
    document有一个叫作'Login'的字符串属性吗? 没有,
    document."Login" 可以得到Login这个form吗?不行。
    既然你传进来的都是id,用document.getElementById(fm)不就行了吗? 而且兼容很好