简体   繁体   English

我可以触发点击href元素而不触发href链接吗?

[英]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.

相关问题 在“ a href”元素上触发点击事件 - Trigger click event on “a href” element <a href>无需点击即可自动链接</a> - <a href> automatic link without click 没有href中的实际链接(href attrbute中的js函数),trigger(&#39;click&#39;)不适用于锚点 - trigger('click') doesn't work on anchors without an actuall link in the href ( js function in the href attrbute ) href点击未在jquery中触发 - a href click not triggering in jquery 在javascript中,在没有JQuery的情况下如何在href =“ javascript:someMethod()”的情况下如何以编程方式单击链接 - In javascript, how can I click on link programmatically in case href=“javascript:someMethod()” without JQuery 我无法单击具有` <a>`标签且没有href属性的链接</a> - I can't click on the link that has `<a>` tag and no href attribute 如何使用 javascript 通过 href 获取元素,然后单击? - How can i get element by href using javascript and after that, click? “ a href”按钮的点击功能不会触发 - On click function for a 'a href' button is not triggering 单击href链接的Javascript - Javascript to click on href link 触发单击事件以使用 jquery 在 href 上动态添加链接 - Trigger click event to dynamically added link on href using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM