在HTML4中,click事件仅为输入元素定义,而某些浏览器(尤其是FF)严格遵循规范.然而HTML5游戏改变了,任何现代浏览器都实现了这个功能,但是 jQuery仍然有一个特殊情况(从jquery触发器方法中提取):
if ( !onlyHandlers && !event.isDefaultPrevented() ) {
if ( (!special._default || special._default.apply( elem.ownerDocument, data ) === false) && !(type === "click" && jQuery.nodeName( elem, "a" )) && jQuery.acceptData( elem ) ) {
// Call a native DOM method on the target with the same name name as the event.
Run Code Online (Sandbox Code Playgroud)
作为解决方法,您可以更改代码:
target.trigger("click");
Run Code Online (Sandbox Code Playgroud)
至
target.get(0).click();
Run Code Online (Sandbox Code Playgroud)
Hiya 工作演示: http ://jsfiddle.net/f6FJ3/19/
对于触发原因,请阅读:jQuery:触发click()不起作用?
It's important to clarify that doing jQuery('#open').click() does not execute the href attribute of an anchor tag so you will not be redirected. It executes the onclick event for #open which is not defined.
下面的代码将在图像点击时获得所需的窗口输出.
JQuery代码
$(document).ready(function(){
$('.tmb').click(function(){
var target=$(this).parent().find("a");
$("#debug").text("clicked "+target.text());
//target.triggerHandler('click');
window.open(target.attr('href'));
});
});?
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,欢呼!
归档时间: |
|
查看次数: |
9761 次 |
最近记录: |