[英]Can I trigger click on href element without triggering href link?
I have a piece of code that will sometimes need to be triggered by a script, but I don't want it to change the URL on my page. 我有一段代码有时需要由脚本触发,但是我不希望它更改页面上的URL。 (it messes up the history state for some odd reason)
(由于某种奇怪的原因,它弄乱了历史状态)
The code I need to trigger: 我需要触发的代码:
$(".photos-bottom .albums #albums li").live("click", function() {
// my action here
});
The code I use to trigger it:
$(".photos-bottom .albums #albums li:first").trigger("click");
The link to click, however I just noticed, it's suppose to click the li and the li is inside of the href so i don't know why it's still clicking it.. 单击链接,但是,我刚刚注意到,它是单击li,而li在href的内部,所以我不知道为什么它仍然单击它。
<a href="#/photos/1/album/42/" rel="address:/photos/1/album/42/">
<li id="42">
try triggerHandler()
instead: http://api.jquery.com/triggerHandler/ 尝试使用
triggerHandler()
代替: http : triggerHandler()
The .triggerHandler() method behaves similarly to .trigger(), with the following exceptions: The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission)... [omissis]
.triggerHandler()方法的行为与.trigger()相似,但以下情况除外:.triggerHandler()方法不会导致事件的默认行为发生(例如,表单提交)... [omissis]
Otherwise use trigger()
with an extraparameter
in the call (see http://api.jquery.com/trigger/ ) and in your handler just check the arguments passed. 否则,请在调用中使用带有
extraparameter
trigger()
(请参阅http://api.jquery.com/trigger/ ),并在处理程序中仅检查传递的参数。 If a parameter was passed then you will prevent the default link action with. 如果传递了参数,则将阻止使用默认链接操作。
<evt>.preventDefault()
(note: I assume you handler is attached to links element, not to the list item, since you cannot wrap a list item into a link) (注意:我假设您将处理程序附加到links元素,而不附加到列表项,因为您无法将列表项包装到链接中)
I think this is caused by live(). 我认为这是由live()引起的。 This is because the events bubble up to the
<body>
and triggers the click event on the <a>
. 这是因为事件冒泡到
<body>
并触发<a>
上的click事件。 You could use delegate() or on() (jQuery > 1.7) to attache the event handler on the <a>
and then return false from the function to stop it from triggering the redirect 您可以使用委托()或on()(jQuery> 1.7)在
<a>
上<a>
事件处理程序,然后从函数返回false,以阻止其触发重定向
You could do 你可以做
$(".photos-bottom .albums #albums a").delegate(".photos-bottom .albums #albums li", "click", function(e) {
// my action here
alert("hi");
//return false so that you stop the immediate propagation of the event
return false;
});
$(".photos-bottom .albums #albums li:first").trigger("click");
Fiddle working with delegate http://jsfiddle.net/3CQFK/ 小提琴与代表http://jsfiddle.net/3CQFK/合作
Yep, it's a normal task. 是的,这是正常的任务。 There's a function you can call on the event which the callback receives which prevents the browser's default action, called preventDefault .
您可以在回调接收到的事件上调用一个函数,该函数阻止浏览器的默认操作,称为preventDefault 。 Your code would be:
您的代码为:
$(".photos-bottom .albums #albums li").live("click", function(event) {
event.preventDefault();
// your code here
// ...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.