链接元素上的 click() 返回 TypeError ...为什么?
我试图弄清楚为什么在 标记元素上执行 click() 函数会返回“undefined_method”的 TypeError。例如,请访问此页面:http://http://www. google.com/#&q=test 。现在,此页面上的每个搜索结果都有一个类名称“l”,因此如果我要求:
document.getElementsByClassName('l')
我会得到所有搜索结果 标记的列表。从逻辑上讲,按如下方式索引该数组会为我提供一个特定的
标记:
document.getElementsByClassName('l')[0]
但是,如果我想单击该链接怎么办?我希望以下内容能够工作
document.getElementsByClassName('l')[0].click()
,但是我得到了 TypeError 'undefined_method'。
为什么?我该如何激活这个链接?
谢谢
I'm trying to figure out why executing a click() function on an <a>
tag element returns a TypeError of 'undefined_method'. For an example, visit this page: http://http://www.google.com/#&q=test . Now, each search result on this page has a class name of 'l', so if I ask for:
document.getElementsByClassName('l')
I get a list of all of the search result <a>
tags. Logically, indexing this array as follows gives me a specific <a>
tag:
document.getElementsByClassName('l')[0]
But, what if I want to click on that link? I would expect the following to work
document.getElementsByClassName('l')[0].click()
But instead I get the TypeError 'undefined_method'.
Why? And how would I activate this link?
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果要将点击事件分派到 DOM 中,则必须使用 W3C DOM dispatchEvent 方法href="http://msdn.microsoft.com/en-us/library/ms536423%28v=VS.85%29.aspx" rel="nofollow">fireEvent 方法供其他人使用。某些浏览器可能两者都不支持。
如果您想处理 HTML5 浏览器,那么您可以调用元素的 click 方法,但是您必须首先测试支持,如果浏览器没有响应,请不要感到惊讶。大多数人认为以编程方式触发元素侦听器/处理程序是一件坏事。
在这种情况下,最好的方法可能是像 Zhehao Maoto 所说的那样,将 window.location 属性设置为 A 元素的 href,或者只是让用户单击他们想要的链接,然后让 HTML 完成它的工作。
编辑
测试表明 IE 支持 A 元素上的单击,以便触发单击处理程序并跟踪链接(如果处理程序不返回 false)。在 Firefox 中,将调用处理程序,但不会跟踪链接。
在 Chrome 中,A 元素不执行任何操作,但按钮会做出响应。 Opera 触发 A 的点击处理程序并跟踪链接。所以最重要的是,支持非常零散(并不令人意外)。
例如
If you want to dispatch a click event into the DOM, you'll have to use the W3C DOM dispatchEvent method for compliant browsers and the MS fireEvent method for others. Some browsers may not support either.
If you want to deal with HTML5 browsers, then you can call the element's click method, however you must test for support first and don't be surprised if browsers don't respond. Most view programatic firing of element listeners/handlers as a bad thing.
Probably the best method in this case is as Zhehao Maoto said and set window.location property to the A element's href, or just let the user click on the link they want and let HTML do its job.
Edit
Testing shows that IE supports click on A elements so that the click handler is fired and the link is followed (if the handler doesn't return false). In Firefox, the handler will be called but the link won't be followed.
In Chrome, an A element does nothing but the button responds. Opera fires the A's click handler and follows the link. So the bottom line is that support is very patchy (not really a surprise).
e.g.
该错误意味着 javascript 锚点对象没有“click()”方法。如果您想以编程方式跟踪链接,最简单的方法就是这样。
这将获取链接的 url(href 属性)并通过设置
window.location
将浏览器重定向到该位置。The error means that javascript anchor objects do not have a "click()" method. If you want to follow the link programmatically, the easiest way would be like so.
This takes the link's url (the href attribute) and redirects the browser to that location by setting
window.location
.除非将操作绑定到
onclick
事件,否则在链接上调用.click()
方法(我相信)不会产生任何效果。您想要类似的内容:
如果您尝试执行的操作是跟随链接,而不是激活
onclick
操作,请执行以下操作:Calling the
.click()
method (I believe) on a link will have no effect unless an action is bound to theonclick
event.You want something like:
If what you're attempting to do is follow the link, rather than activate an
onclick
action, do this: