blur()vs. onblur() [英] blur() vs. onblur()
问题描述
我有一个带有onblur事件监听器的输入标签:
I have a input tag with an onblur event listener:
<input id="myField" type="input" onblur="doSomething(this)" />
通过JavaScript,我想触发此输入上的模糊事件,以便它反过来调用 doSomething
函数。
Via JavaScript, I want to trigger the blur event on this input so that it, in turn, calls the doSomething
function.
我最初想的是调用模糊:
My initial thought is to call blur:
document.getElementById('myField').blur()
但这不起作用(虽然没有错误)。
But that doesn't work (though no error).
这样做:
document.getElementById('myField').onblur()
这是为什么? .click()
将通过onclick监听器调用附加到元素的click事件。为什么 blur()
的工作方式不一样?
Why is that? .click()
will call the click event attached to an element via the onclick listener. Why does blur()
not work the same way?
推荐答案
这个:
document.getElementById('myField').onblur();
的作用是因为你的元素(< input>
)有一个名为onblur的属性,其值是一个函数。因此,你可以调用它。然而,您不告诉浏览器模拟实际的模糊事件;例如,没有创建事件对象。
works because your element (the <input>
) has an attribute called "onblur" whose value is a function. Thus, you can call it. You're not telling the browser to simulate the actual "blur" event, however; there's no event object created, for example.
元素没有模糊属性(或方法或其他),所以这就是为什么第一件事没有'工作。
Elements do not have a "blur" attribute (or "method" or whatever), so that's why the first thing doesn't work.
这篇关于blur()vs. onblur()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!