插件以使方法“点击”跨浏览器 [英] plugin to make the method "click" cross-browser
问题描述
我做了这个插件,使方法'[0] .click()'跨浏览器,但我无法在Firefox中工作,我在Firefox中的当前版本是3 .6。 16.其他浏览器(Opera / Chrome / Safari / IE)的工作效果很好。
I made this plugin to make the method '[0].click()' cross-browser, but I can not work in Firefox, the current version I have in Firefox is 3 .6. 16. The other browsers (Opera / Chrome / Safari / IE) works well.
HTML:
<a href="#" id="myanchor">z</a>
<ul>
<li id="1">1</li>
<li id="2">2</li>
<li id="3">3</li>
</ul>
Javascript:
Javascript:
jQuery.fn.runClick = function () {
var element = jQuery(this).get(0);
if (jQuery.browser.msie) { // IE
element.click();
}
else {
//var evt = document.createEvent("HTMLEvents");
//evt.initEvent('click', true, true);
var evt = element.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
element.dispatchEvent(evt);
}
return this;
};
jQuery(function() {
jQuery('li').bind('click', function(event) {
var
key = jQuery(this).attr('id');
jQuery('#myanchor').attr('href', 'www.mydomaind.com/x/?id=' + key).runClick();
});
});
谢谢。
编辑
我根据评论和回复扩展了一个例子
I extended an example based on the comments and responses
EDIT II
类似问题:
如何-DO-I-编程键式上的元件功能于火狐
浏览器友好-way-to-simulate-anchor-click-with-jquery
推荐答案
显然,点击Firefox中的链接不启动它:
Apparently, clicking a link in Firefox does not fire it:
jQuery('#myanchor').click(function(){
alert("I was clicked, but I'll do nothing about it");
});
jQuery("li").click(function(){
var key = jQuery(this).attr('id');
jQuery('#myanchor').attr("href", "http://www.mydomaind.com/x/?id=" + key).click();
});
我看不到你的大图,所以你可能有一个有效的理由去做。但是,如果所有这一切的唯一目的是转到另一页面,直接执行:
I can't see your big picture so you may have a valid reason to do it. But if the only purpose of all this is to go to another page, do it directly:
jQuery("li").click(function(){
top.location.href = "http://www.mydomaind.com/x/?id=" + this.id;
});
ID不应以数字开头。
P.S. IDs should not start with a digit.
这篇关于插件以使方法“点击”跨浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!