Javascript 模糊()与onblur()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5212651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
blur() vs. onblur()
提问by DA.
I have a input tag with an onblur event listener:
我有一个带有 onblur 事件侦听器的输入标签:
<input id="myField" type="input" onblur="doSomething(this)" />
Via JavaScript, I want to trigger the blur event on this input so that it, in turn, calls the doSomething
function.
通过 JavaScript,我想在此输入上触发模糊事件,以便它反过来调用该doSomething
函数。
My initial thought is to call blur:
我最初的想法是调用模糊:
document.getElementById('myField').blur()
But that doesn't work (though no error).
但这不起作用(虽然没有错误)。
This does:
这样做:
document.getElementById('myField').onblur()
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?
这是为什么?.click()
将通过 onclick 侦听器调用附加到元素的 click 事件。为什么不blur()
以同样的方式工作?
回答by Pointy
This:
这个:
document.getElementById('myField').onblur();
works because your element (the <input>
) has an attribute called "onblur" whose value is a function. Thus, you can call it. You're nottelling the browser to simulate the actual "blur" event, however; there's no event object created, for example.
之所以有效,是因为您的元素 (the <input>
) 有一个名为“onblur”的属性,其值为一个函数。因此,您可以调用它。但是,您并没有告诉浏览器模拟实际的“模糊”事件;例如,没有创建事件对象。
Elements do not have a "blur" attribute (or "method" or whatever), so that's why the first thing doesn't work.
元素没有“模糊”属性(或“方法”或其他什么),所以这就是为什么第一件事不起作用。
回答by Elian Ebbing
Contrary to what pointy says, the blur()
method does exist and is a part of the w3c standard. The following exaple will work in every modern browser (including IE):
与 pointy 所说的相反,该blur()
方法确实存在并且是w3c 标准的一部分。以下示例适用于所有现代浏览器(包括 IE):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Javascript test</title>
<script type="text/javascript" language="javascript">
window.onload = function()
{
var field = document.getElementById("field");
var link = document.getElementById("link");
var output = document.getElementById("output");
field.onfocus = function() { output.innerHTML += "<br/>field.onfocus()"; };
field.onblur = function() { output.innerHTML += "<br/>field.onblur()"; };
link.onmouseover = function() { field.blur(); };
};
</script>
</head>
<body>
<form name="MyForm">
<input type="text" name="field" id="field" />
<a href="javascript:void(0);" id="link">Blur field on hover</a>
<div id="output"></div>
</form>
</body>
</html>
Note that I used link.onmouseover
instead of link.onclick
, because otherwise the click itself would have removed the focus.
请注意,我使用了link.onmouseover
而不是link.onclick
,否则点击本身会移除焦点。
回答by cusimar9
I guess it's just because the onblur event is called as a resultof the input losing focus, there isn't a bluraction associated with an input, like there is a clickaction associated with a button
我想这只是因为 onblur 事件是由于输入失去焦点而被调用的,没有与输入相关联的模糊动作,就像与按钮相关联的点击动作一样