我的 javascript onblur 函数不起作用

发布于 2024-12-25 19:01:33 字数 1043 浏览 5 评论 0原文

我正在处理的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神爱温柔 2025-01-01 19:01:33

您的问题是:child.onblur = BlurPrueba(),您立即执行blurPrueba。应该是一个参考:child.onblur = BlurPrueba

更改该行,告诉浏览器:“在子元素模糊时,激活 blurPrueba 功能”。

如果您使用 blurPrueba() 激活该函数并将其结果分配给模糊事件,则 blurPrueba() 不会返回任何内容。所以你的行实际上是这样写的:“onblur = undefined

总之,如果你希望浏览器处理一个事件(这里是blur),你需要提供一个引用 到处理函数(此处为 blurPrueba)。

Your problem is: child.onblur = blurPrueba(), where you execute blurPrueba 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 (here blurPrueba).

微暖i 2025-01-01 19:01:33

更改

child.onblur = BluPrueba();

child.onblur = function(){blurPrueba()};

Change

child.onblur = blurPrueba();

to

child.onblur = function(){blurPrueba()};

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文