Javascript 当按钮单击时,在google chrome扩展中更改文本框值
我的代码如下所示:Javascript 当按钮单击时,在google chrome扩展中更改文本框值,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我的代码如下所示: document.getElementById('Login').onClick=LogIn; function LogIn(){ document.getElementById('UserName').value='test'; document.getElementById('Password').value='test2'; } <!DOCTYPE html> <html> <head> <META http-equiv
document.getElementById('Login').onClick=LogIn;
function LogIn(){
document.getElementById('UserName').value='test';
document.getElementById('Password').value='test2';
}
<!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>
当我点击按钮时,我得到了异常
未捕获的TypeError:无法将属性“onClick”设置为null
我的html代码如下所示:
document.getElementById('Login').onClick=LogIn;
function LogIn(){
document.getElementById('UserName').value='test';
document.getElementById('Password').value='test2';
}
<!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;
}
它找不到您的ID元素
登录
,请为您的按钮张贴HTML标记。这是你的代码,正如下面的答案所示,它是onclick
而不是onclick
:你能发布代码放置位置的完整上下文吗。更新帖子。添加了html代码。这是chrome的扩展吗?@tymeJV发布的fiddle假设代码是执行的onload
,如果您的代码只是包含在页面中而不是chrome扩展,那么这显然是问题的解决方案。如果您的代码在doktormdcore.js
中,那么答案是您需要将执行延迟到load
或DOM ready
事件,因为当您的代码按原样执行时,Login
将不存在。如何在纯javascript中检查DOM ready或not?已尝试。但还是一样的例外。