简体   繁体   English

单击按钮时,更改Google Chrome扩展程序中的文本框值

[英]When button clicked change textbox value in google chrome extension

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 未被捕获的TypeError:无法将属性'onClick'设置为null

My html code is shown below: 我的html代码如下所示:

<!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. 我不想使用jQuery。

You need to change onClick to onclick . 您需要将onClick更改为onclick JavaScript is case-sensitive. JavaScript区分大小写。

Edit 编辑

I am still not sure if your code is an extension or not but here is how you could ensure that the page is fully loaded if you attach your code to the onload event. 我仍然不确定您的代码是否为扩展名,但是如果将代码附加到onload事件中,可以通过以下方法确保页面已完全加载。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM