我的 javascript onblur 函数不起作用
我正在处理的 javascipt 函数遇到一些问题。
这是我尝试使用该函数执行的操作:
我有一个具有给定值的表元素,当单击它时,它会调用我的javasript函数,该函数应该为appendChild一个带有该元素值的INPUT元素,因此用户可以更改该值。我希望 INPUT 元素通过 onblur()
事件调用函数,以便修改后的值可以再次显示在表元素上。
我的问题是该元素不遵守 onblur()
事件。该函数在创建 Input 元素后立即执行,并且不会等待 onblur()
事件发生。
这是两个函数的代码:
var elemento = true;
function prueba(clave,cantidad) {
if(elemento){
var percent = document.getElementById('porciento' + clave);
percent.innerHTML = "";
var input = document.createElement("input");
input.setAttribute('type','text');
input.setAttribute('size','5');
input.setAttribute('value',cantidad);
input.setAttribute('id','child'+clave);
percent.appendChild(input);
input.focus();
child = document.getElementById("child" + clave);
child.onblur = blurPrueba();
}
}
function blurPrueba() {
if(elemento)
alert("Hello");
}
显示警报而不是 onblur()
有谁知道为什么会发生这种情况???
I am having some problems with a javascipt function which I'm working on.
Here is what I am trying to do with the function:
I have a table element with a given value, and when there is a click on it, it calls my javasript function which is supose to appendChild an INPUT element with the value of the element, so the user can change that value. I want the INPUT element to call a function whit the onblur()
event, so the modified value could be display again on the table element.
My problem is that the element does not respect the onblur()
event. The function is executed right after the Input element is created, and does not wait to be an onblur()
event.
Here is the code of the two functions:
var elemento = true;
function prueba(clave,cantidad) {
if(elemento){
var percent = document.getElementById('porciento' + clave);
percent.innerHTML = "";
var input = document.createElement("input");
input.setAttribute('type','text');
input.setAttribute('size','5');
input.setAttribute('value',cantidad);
input.setAttribute('id','child'+clave);
percent.appendChild(input);
input.focus();
child = document.getElementById("child" + clave);
child.onblur = blurPrueba();
}
}
function blurPrueba() {
if(elemento)
alert("Hello");
}
The alert is displayed without being an onblur()
Does anyone knows why this is happening???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是:
child.onblur = BlurPrueba()
,您立即执行blurPrueba
。应该是一个参考:child.onblur = BlurPrueba
更改该行,告诉浏览器:“在子元素模糊时,激活
blurPrueba
功能”。如果您使用
blurPrueba()
激活该函数并将其结果分配给模糊事件,则blurPrueba()
不会返回任何内容。所以你的行实际上是这样写的:“onblur = undefined
”总之,如果你希望浏览器处理一个事件(这里是
blur
),你需要提供一个引用 到处理函数(此处为blurPrueba
)。Your problem is:
child.onblur = blurPrueba()
, where you executeblurPrueba
immediately. Should be a reference:child.onblur = blurPrueba
Changing the line, you tell the browser: "on blur for the child element, activate the
blurPrueba
function".If you use
blurPrueba()
you activate the function and assign it's result to the blur event,blurPrueba()
doesn't return anything. So your line actually says: "onblur = undefined
"In summary, if you want the browser to handle an event (here
blur
) you need to provide a reference to the handler function (hereblurPrueba
).更改
child.onblur = BluPrueba();
到
child.onblur = function(){blurPrueba()};
Change
child.onblur = blurPrueba();
to
child.onblur = function(){blurPrueba()};