admin管理员组文章数量:1435090
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://;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
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./#&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
Share Improve this question asked Jul 6, 2011 at 1:28 mixmix 7,16115 gold badges65 silver badges94 bronze badges3 Answers
Reset to default 1If you want to dispatch a click event into the DOM, you'll have to use the W3C DOM dispatchEvent method for pliant 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.
<!-- Call click() on link to Google -->
<a href="#" onclick="
var el = document.getElementById('a0');
if ('click' in el) {
alert('found click\n' + el.click);
el.click();
}">Click link</a>
<!-- Call click on button with onclick handler -->
<a href="#" onclick="
var el = document.getElementById('b0');
if ('click' in el) {
alert('found click\n' + el.click);
el.click();
}">Click button</a>
<a href="http://www.google." id="a0">Google</a>
<button onclick="alert('click!!');" id="b0">button</button>
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.
window.location = a.href; // assuming a is an anchor object
This takes the link's url (the href attribute) and redirects the browser to that location by setting window.location
.
Calling the .click()
method (I believe) on a link will have no effect unless an action is bound to the onclick
event.
You want something like:
document.getElementsByClassName('l')[0].onclick();
If what you're attempting to do is follow the link, rather than activate an onclick
action, do this:
window.location.href = document.getElementsByClassName('l')[0].getAttribute('href');
本文标签: javascriptclick() on a link element returns a TypeErrorwhyStack Overflow
版权声明:本文标题:javascript - click() on a link element returns a TypeError ... why? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745648021a2668253.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论