[英]I get an undefined error in my error console
My myclickHandler function works perfectly but when I go on to my error console I get this error: 我的myclickHandler函数运行正常,但当我继续我的错误控制台时,我收到此错误:
document.getElementsByName("prequestion")[0] is undefined;
document.getElementsByName(“prequestion”)[0]未定义;
Below is my code 以下是我的代码
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Create a Session</title>
<script type="text/javascript">
document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
function myClickHandler(){
if(validation()){
openSessionPopup();
}
</script>
</head>
<body>
<form action="create_session.php" method="post" name="sessionform">
<p><strong>10: </strong><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="myClickHandler()"/></p>
</form>
</body>
My question is what do I need to remove this error? 我的问题是我需要删除此错误? because my function works does it matter if it shows an error in the error console?
因为我的函数工作是否重要,如果它在错误控制台中显示错误?
Chances are you're loading the javascript before the DOM (or even possibly the HTML) has loaded properly. 您可能在DOM(甚至可能是HTML)正确加载之前加载了javascript。
For example, the following reproduces the same error as described in your question : 例如,以下内容会再现与您的问题中描述的相同的错误:
<script type="text/javascript">
document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
function myClickHandler(){
alert('test');
}
</script>
<input type="" name="prequestion" />
Whereas the following does not. 而以下情况并非如此。 The error is not present and the code works fine.
错误不存在,代码工作正常。
<input type="" name="prequestion" />
<script type="text/javascript">
document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
function myClickHandler(){
alert('test');
}
</script>
Trying to attach the event listener to an element which doesn't technically exist yet causes problems. 尝试将事件侦听器附加到技术上不存在的元素会导致问题。 You can check for the browser reporting the page has been loaded before attaching the event if needs be but be aware that browsers report this in different ways.
如果需要,您可以在附加事件之前检查已加载页面的浏览器报告,但请注意浏览器以不同方式报告此情况。 Librarys such as jQuery provide useful means to test this such as
$(document).ready(function(){});
诸如jQuery之类的库提供了有用的方法来测试它,例如
$(document).ready(function(){});
Edit: I've just seen that you've added the relevant HTML too. 编辑:我刚刚看到你也添加了相关的HTML。 It appears that this is in fact the case and the reason you're still seeing the expected behavior is because you're also using the
onclick=""
attribute on the input element. 事实上这似乎是这种情况,并且你仍然看到预期行为的原因是因为你还在输入元素上使用
onclick=""
属性。 This is where the function is being called on click, and the event handler isn't being attached as you expect. 这是在单击时调用函数的位置,并且未按预期附加事件处理程序。
Edit #2: You also appear to be missing a closing }
for your function. 编辑#2:您似乎也缺少函数的结束
}
。 And I've now tested with your exact HTML and moving the script to just above the body tag resolves the error and correctly attaches the function to the click event. 我现在已经使用您的确切HTML进行了测试,并将脚本移动到body标签上方可以解决错误并正确地将函数附加到click事件。
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Create a Session</title>
</head>
<body>
<form action="create_session.php" method="post" name="sessionform">
<p><strong>10: </strong><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" /></p>
</form>
<script type="text/javascript">
document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
function myClickHandler(){
alert('test');
} // This was missing
</script>
</body>
I suspect that the problem is that there are no elements with the name attribute set to "prequestion". 我怀疑问题是没有name属性设置为“prequestion”的元素。
If you're getting that error, then your script doesn't actually work, so you should fix it. 如果您收到该错误,那么您的脚本实际上不起作用,因此您应该修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.