jQuery 模拟原生点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1786377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulate native click
提问by scribu
I need to trigger the click event on a link and then, if none of the listeners prevented the default, change the location.
我需要在链接上触发点击事件,然后,如果没有任何侦听器阻止默认设置,则更改位置。
This is what I've got so far:
这是我到目前为止所得到的:
var $el = $('a#my_special_link');
var onClick = $el.attr('onclick');
// Hack to capture the event object
var ev_reference;
var ev_capture = function(ev) { ev_reference = ev; }
$el.bind('click', ev_capture);
// Don't leave out the onClick handler
if ( typeof onClick == 'function' )
$el.bind('click', onClick);
$el.trigger('click');
// Clean up
$el.unbind('click', ev_capture);
if ( typeof onClick == 'function' )
$el.unbind('click', onClick);
// Redirect if necessary
if ( ! ev_reference.isDefaultPrevented() )
window.location = $el.attr('href');
Any suggestions for improvement are welcome.
欢迎提出任何改进建议。
PS:Yes, it's just how a regular link would work, except you can do some other processing in between.
PS:是的,这就是常规链接的工作方式,除非您可以在两者之间进行一些其他处理。
PPS:No, just doing $el.trigger('click'); will notchange the location.
PPS:不,只是做 $el.trigger('click'); 会不会改变位置。
回答by Mark Schultheiss
enter code here
To extract my notes to others posts: (another way to say same basic stuff)
enter code here
将我的笔记提取到其他帖子中:(另一种说相同基本内容的方式)
<a href="somewhere" id="mylink">MyLink</a>
<script>jQuery('#mylink').click(function(){
// do whatever I want here, then redirect
window.location.href = "http://stackoverflow.com";
});
function doClick(){
jQuery('#mylink').trigger('click');
};
// trigger the redirect now
doClick();
</script>
EDIT1:@ comments:
EDIT1:@ 评论:
If the "other" you describe returns false, you at that point, terminate the callback with the false - so you are correct but has nothing to do with this handler. Which ever one hits first executes, and the other never does return, and this one redirects prior to the other one hitting the log if it executes first.
如果您描述的“其他”返回 false,那么此时您将使用 false 终止回调 - 所以您是正确的,但与此处理程序无关。先执行哪个先执行,另一个永远不会返回,如果第一个执行,则此一个在另一个命中日志之前重定向。
In order to put your log in, you put that where I have my
为了把你的登录,你把它放在我有我的地方
"// do whatever I want here, then redirect"
comment To remove the other event, then bind with this one only:
评论要删除另一个事件,然后只绑定这个:
<a href="somewhere" id="mylink">MyLink</a>
<script>jQuery('#mylink').unbind('click').bind('click', function(){
// do whatever I want here, then redirect
window.location.href = "http://stackoverflow.com";
});
function doClick(){
jQuery('#mylink').trigger('click');
};
// trigger the redirect now
doClick();
回答by ChaosPandion
http://docs.jquery.com/Events/trigger#eventdata
http://docs.jquery.com/Events/trigger#eventdata
$('a#my_special_link').trigger("click");
回答by David Hellsing
"if none of the listeners prevented the default, change the location."
“如果没有听众阻止默认设置,请更改位置。”
Sounds exactly how a normal link behaves...?
听起来正是正常链接的行为......?
Anyway, if you are trying to look for "return false", it will not be found using isDefaultPrevented()
, which only will return true if preventDefault()
is called from the same event object. In this example, I'm sending the event object to whatever()
and if that function sets preventDefault()
, the window will relocate anyway (is that what you need...?)
无论如何,如果您尝试查找“返回假”,则不会使用 找到isDefaultPrevented()
它,只有在preventDefault()
从同一事件对象调用时才会返回真。在这个例子中,我将事件对象发送到whatever()
,如果该函数设置preventDefault()
,窗口无论如何都会重新定位(这是你需要的......?)
$('a#my_special_link').bind('click', function(e) {
whatever(e);
if (e.isDefaultPrevented()) {
window.location = $(e.target).attr('href');
}
});
回答by mtyaka
If I understand your question correctly, you have an a
element that you don't control, which has an onclick
attribute that returns false
. Returning false
from an onclick handler prevents a click on the link from taking you to the linked page. And you don't want that.
如果我正确理解您的问题,那么您有一个a
您无法控制的元素,它的onclick
属性返回false
. false
从 onclick 处理程序返回可防止单击链接将您带到链接页面。而你不想那样。
You could store the onclick
handler function and then remove it from the link element:
您可以存储onclick
处理程序函数,然后将其从链接元素中删除:
var $a = $('#my_special_link');
var onclickHandler = $a.attr('onclick');
$a.removeAttr('onclick');
Then cal the stored handler function in your custom click handler:
然后在您的自定义点击处理程序中调用存储的处理程序函数:
$a.click(function() {
// ...
onclickHandler();
// ...
});