0

My code is shown below:

document.getElementById('Login').onClick=LogIn;
function LogIn(){
document.getElementById('UserName').value='test';
document.getElementById('Password').value='test2';
 }

When i click button i got exception

Uncaught TypeError: Cannot set property 'onClick' of null

My html code is shown below:

<!DOCTYPE html>
<html>
<head>  
<META http-equiv=content-type content=text/html;charset=utf8>
<META http-equiv=content-type content=text/html;charset=windows-1254>
<META http-equiv=content-type content=text/html;charset=x-mac-turkish>
<script src="doktormdcore.js"></script>
<title>Test</title>

</head>
<body>
<table>
  <tr>
    <td> UserName:</td>
    <td>
      <input type="text" id="UserName" size="20" />
    </td>
  </tr>
  <tr>
    <td>Password:</td>
    <td>
      <input type="password" id="Password" />
    </td>
  </tr>
  <tr>
    <td></td>
<td><input type="button" value="Enter" id="Login"  /></td>
  </tr>
 </table>
 </body>
 </html>

What i must supposed to do. I dont want to use jquery.

4

1 回答 1

0

您需要更改onClickonclick. JavaScript 区分大小写。

编辑

我仍然不确定您的代码是否是扩展程序,但如果您将代码附加到onload事件中,您可以通过以下方式确保页面已完全加载。

  function LogIn(){
    document.getElementById('UserName').value='test';
    document.getElementById('Password').value='test2';
  }
  window.onload = function(){
    document.getElementById('Login').onclick=LogIn;
  }
于 2013-07-22T14:30:33.307 回答