[英]Strange behavior - getElementById()
I'm currently working on something simple - a JavaScript script that makes some minimal validation if a string is an email. 我目前正在研究一些简单的东西-一个JavaScript脚本,如果字符串是电子邮件,它会进行一些最小限度的验证。
But a problem occurred and I can't seem to understand it. 但是出现了一个问题,我似乎无法理解。
I have an input text field, where the user enters an email, a button, which when clicked, get's the string from the input and displays it in a div with a green background, if valid, or a red one - if invalid. 我有一个输入文本字段,用户在其中输入电子邮件和一个按钮,单击该按钮时,将从输入中获取字符串,并将其显示在具有绿色背景(如果有效)或div(如果无效)中的div中。
So, I have made a function get(), which simply does return a DOM element with on given id. 因此,我做了一个函数get(),它确实返回了具有给定id的DOM元素。 Nothing fancy, but the strange thing comes next. 没什么好想的,但接下来是奇怪的事情。
When I try to use an element, which I've 'got' from the DOM with this function in a callback function, I simply can't. 当我尝试使用一个元素(该元素是我在回调函数中通过此函数从DOM中获得的)时,我简直不能这样做。 But when in the callback function I manually get it with document.getElementById('status') , it works. 但是在回调函数中,我可以通过document.getElementById('status')手动获取它,它可以工作。
I don't understand where is the problem. 我不明白问题出在哪里。
Here's my Html : 这是我的HTML :
<section>
<input type="text" name="email" placeholder="Enter an email" id="input" />
<input type="submit" value="Validate →" id="submit" />
<div class="status" id="status"></div>
</section>
And here's the JS : 这是JS :
var get = function(id) {
return document.getElementById(id);
};
var input = get('input'),
status = get('status'),
btn = get('submit');
btn.addEventListener('click', function(){
// doesn't work
status.innerHTML = 'Checking...';
// works?!
// Puts 'Checking' text in div#status
document.getElementById('status').innerHTML = 'Checking...';
});
It is a bit nasty problem where a global variable with name status
won't take non string values... if you try to assign one then it will take the toString() value of that object(have tested in chrome). 这是一个令人讨厌的问题,其中具有名称status
的全局变量不会采用非字符串值...如果尝试分配一个值,则它将采用该对象的toString()值(已在chrome中测试)。
In your console if you log the value of status
you should get [object HTMLDivElement]
which is the toString() implementation of Element
object. 在控制台中,如果记录status
的值,则应获取[object HTMLDivElement]
,它是Element
对象的toString()实现。
Just rename the variable and you should be fine 只需重命名变量,就可以了
var get = function(id) {
return document.getElementById(id);
};
var input = get('input'),
status1 = get('status'),
btn = get('submit');
btn.addEventListener('click', function(){
// doesn't work
status.innerHTML = 'Checking...';
// works?!
// Puts 'Checking' text in div#status
document.getElementById('status').innerHTML = 'Checking...';
});
The same is true for other key window properties like name
其他关键窗口属性(如name
也是如此
You should put this code 你应该把这个代码
window.onload = function() {
var input = get('input'),
status = get('status'),
btn = get('submit');
btn.addEventListener('click', function(){
// doesn't work
status.innerHTML = 'Checking...';
// works?!
// Puts 'Checking' text in div#status
document.getElementById('status').innerHTML = 'Checking...';
});
};
it should work. 它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.