点击()在IE中工作,但不是Firefox [英] Click() works in IE but not Firefox

查看:153
本文介绍了点击()在IE中工作,但不是Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(document).ready(function( ){
$('li#first')。click();
});

我也试过:

<$ p $ 。p> 的document.getElementById( '第一')点击();

但这也行不通。



<这是一个IE错误/功能,或者是其他浏览器不支持的 click()
$ b

响应评论:


  1. 首先有一个ID
    的元素,不再有。

  2. 这是列表元素上的onclick,用于展开元素并将焦点移到Google Map元素上。
  3. 在patrick的响应中运行代码添加另一个点击事件的元素)产生了一些有趣的行为。当运行 $('li#first')。click()时,只有新的事件触发,但用鼠标点击元素(新的和原始的) / li>

在此先感谢您的支持。

解决方案

Firefox不支持click()。
$ b 运行 document.getElementById('first')。click()返回以下错误 click不是函数



所以我添加了一段代码来添加click()到每一个元素。 此代码是在一系列令人痛苦的谷歌搜索后发现的此主题




$ b

  HTMLElement.prototype.click = function(){>这段代码在下面,只需要在页面上包含一次。 
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click',true,true,this.ownerDocument.defaultView,1,0,0,0,0,false,false,false,0,null);
this.dispatchEvent(evt);
}


I have code which is trivial but only works in IE not Firefox.

$(document).ready(function(){
    $('li#first').click();
});

I have also tried:

document.getElementById('first').click();

But that doesn't work either.

Is this an IE bug/feature or is click() not supported in the other browsers?

Responding to comments:

  1. There is a single element with ID first, no more.
  2. It is an onclick on the list element that expands the element and moves focus on a Google Map element.
  3. Running the code in patrick's response (adding another click event to the element) produced some interesting behaviour. When running $('li#first').click() only the new event fired, but physically clicking the element with the mouse fired both (new and original).

Thanks in advance.

解决方案

Firefox does not support click().

Running document.getElementById('first').click() returns the following error click is not a function

So I added a snippet of code to add click() functionality to every element. This code was found after a painful series of google searches resulting in this thread.

The snippet is below and needs to be included just once on the page:

HTMLElement.prototype.click = function() {
   var evt = this.ownerDocument.createEvent('MouseEvents');
   evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
   this.dispatchEvent(evt);
} 

这篇关于点击()在IE中工作,但不是Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆