Javascript 'undefined' 不是在 Safari 中评估 el.click() 的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12744202/
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
'undefined' is not a function evaluating el.click() in Safari
提问by Hypn0tizeR
I would like to run a js function on pressing Enter button. Everything works great in IE, Chrome and Opera, but I get an error (explained below) in Safari.
我想在按下 Enter 按钮时运行一个 js 函数。在 IE、Chrome 和 Opera 中一切正常,但在 Safari 中出现错误(解释如下)。
I need a pure Javascriptsolution, not jQuery.
我需要一个纯 Javascript解决方案,而不是 jQuery。
This is the function:
这是功能:
function pressSearch(e) {
if (typeof e == 'undefined' && window.event) {
e = window.event;
}
var charCode = (e.which) ? e.which :
((e.charCode) ? e.charCode :
((e.keyCode) ? e.keyCode : 0));
if (charCode == 13) {
document.getElementById('enterSearch').click();
}
}
This is the HTML:
这是HTML:
<input type="text" id="searchKey" onkeypress="pressSearch(event)">
<div id="enterSearch">Search</div> // This is the "button"
I get this error:
我收到此错误:
// Safari
TypeError: 'undefined' is not a function
(evaluating 'getElementById('enterSearch').click()')
What am I doing wrong?
我究竟做错了什么?
EDIT:I've fixed the Mozilla Firefox error.
编辑:我已经修复了 Mozilla Firefox 错误。
回答by lqc
If you take a look at the HTML DOM Level 2 Specification, the click()
function is only defined for HTMLInputElement
, so while certainly not very user-friendly Safari doesn't need to implement that.
如果您查看HTML DOM Level 2 Specification,该click()
函数仅针对 定义HTMLInputElement
,因此虽然对用户不是很友好,但 Safari 不需要实现它。
The correct way to trigger an event for modern browsers is in DOM Level 3 Events:
现代浏览器触发事件的正确方法是在DOM Level 3 Events 中:
// First create an event
var click_ev = document.createEvent("MouseEvents");
// initialize the event
click_ev.initEvent("click", true /* bubble */, true /* cancelable */);
// trigger the event
document.getElementById("someElement").dispatchEvent(click_ev);
Here's jsfiddle that works in Chrome, Safari, Firefox and IE9: http://jsfiddle.net/y5yW9/6/
这是适用于 Chrome、Safari、Firefox 和 IE9 的 jsfiddle:http: //jsfiddle.net/y5yW9/6/
回答by Daedalus
Safari is treating (I take that back; .click()
as undefined as .click()
does not exist for Safari..click()
is working for me. I don't know then :/) I came across this same issue while working on a similar problem. What I came up with, after a bit of research and such, is the following for triggering click events in safari(as well as other browsers, of course):
Safari 将(我收回;.click()
未定义视为.click()
Safari 不存在。.click()
正在为我工作。那时我不知道:/)我在处理类似问题时遇到了同样的问题。经过一些研究等,我想出了以下用于在 safari(当然还有其他浏览器)中触发点击事件的方法:
var t = document.getElementById('someidhere');
if (document.dispatchEvent) {
var o = document.createEvent('MouseEvents');
o.initMouseEvent('click', true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, t);
t.dispatchEvent(o)
} else if (document.fireEvent) {
t.fireEvent('onclick');
} else if (t.click()) {
t.click()
}
I don't know if this will work for your firefox side of things, given that according to @Dennis, your code already works for firefox. Without an environment to really test this in, I'm shooting in the dark. I hope it works for you.
我不知道这是否适用于您的 Firefox 方面,因为根据@Dennis,您的代码已经适用于 Firefox。没有一个环境来真正测试这个,我在黑暗中拍摄。我希望这个对你有用。
回答by Matt Whipple
You're triggering a click event on a div
which would have no implicit click handler.
您正在触发一个div
没有隐式点击处理程序的点击事件。
回答by DEAD10CC
A div
element has no click event handler defined per default. So, first you need to apply a click event for the enterSearch div, e.g. <div id="enterSearch" onclick="submitSearch()">Search</div>
. Otherwise the button itself won't be clickable.
一个div
元素都有每默认定义没有click事件处理程序。因此,首先您需要为 enterSearch div 应用点击事件,例如<div id="enterSearch" onclick="submitSearch()">Search</div>
. 否则按钮本身将无法点击。
To execute an event programmatically, use this construct:
要以编程方式执行事件,请使用以下构造:
function pressSearch(e) {
if (typeof e == 'undefined' && window.event) {
e = window.event;
}
var charCode = (e.which) ? e.which : ((e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : 0));
if (charCode == 13) {
var a = document.getElementById('enterSearch');
if (typeof a.onclick == "function") {
a.onclick.apply(a); // binds the scope of onclick to a
}
}
}
I could not validate this for IE, but it works with Safari.
我无法为 IE 验证这一点,但它适用于 Safari。
回答by xdazz
getElementById('enterSearch')
getElementById('enterSearch')
should be document.getElementById('enterSearch')
应该 document.getElementById('enterSearch')