plugin to make the method "click" cross-browser
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:
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();
});
});
based on this question
thanks.
EDIT
I extended an example based on the comments and responses
EDIT II
similar questions:
how-do-i-programmatically-click-on-an-element-in-firefox
browser-friendly-way-to-simulate-anchor-click-with-jqueryYou using jQuery, but not using jQuery's .click()
method ...why?
var $elements = $('some-jquery-selector');
$elements.click(); // that's it.
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;
});
P.S. IDs should not start with a digit.
精彩评论