繁体   English   中英

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

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

我的代码如下所示:

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

当我单击按钮时,出现异常

未被捕获的TypeError:无法将属性'onClick'设置为null

我的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>

我必须做的。 我不想使用jQuery。

您需要将onClick更改为onclick JavaScript区分大小写。

编辑

我仍然不确定您的代码是否为扩展名,但是如果将代码附加到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