admin管理员组文章数量:1244432
I have a button that's located in an anchor, and that button has some logic that's triggerd by clicking
on it.
The problem is that whenever I click on that button, the app get's redirected due the anchor
parent element.
<a href="foo" id="bar">
<span id="button">click me</span>
</a>
I tried using .stopPropagation() like it's mentioned in this post, however it doesn't work.
I tried:
$('#button').on('click', function(e) {
e.stopPropagation();
})
Here's the fiddle.
However, if I replace the parent anchor
with a div
, then it works - JSFiddle
Am I doing something wrong?
Update: I know I can prevent redirecting with e.preventDefault()
, however, I want when I click on the parent element to redirect, and when I click on the button not to redirect and start doing my JS functions (open modal).
I have a button that's located in an anchor, and that button has some logic that's triggerd by clicking
on it.
The problem is that whenever I click on that button, the app get's redirected due the anchor
parent element.
<a href="foo" id="bar">
<span id="button">click me</span>
</a>
I tried using .stopPropagation() like it's mentioned in this post, however it doesn't work.
I tried:
$('#button').on('click', function(e) {
e.stopPropagation();
})
Here's the fiddle.
However, if I replace the parent anchor
with a div
, then it works - JSFiddle
Am I doing something wrong?
Update: I know I can prevent redirecting with e.preventDefault()
, however, I want when I click on the parent element to redirect, and when I click on the button not to redirect and start doing my JS functions (open modal).
-
Use
e.preventDefault()
– hampusohlsson Commented Oct 21, 2015 at 14:54 - 2 This is a duplicate of so many questions, use the search. – Etheryte Commented Oct 21, 2015 at 14:55
-
@Nit yes, I seached a lot but I didn't found a solution when the parent is an
anchor
element. – Vucko Commented Oct 21, 2015 at 14:57 - What is the purpose of differentiating between clicking on the span vs clicking on the anchor tag wrapped around it? – AtheistP3ace Commented Oct 21, 2015 at 14:59
-
1
It seems that the button click should trigger anchor
href
redirection. If you want the button to have different action then move it outside the anchor. – MaxZoom Commented Oct 21, 2015 at 15:00
6 Answers
Reset to default 7Try this:
$('#bar').on('click', function(e) {
if( $(e.target).is('#button') ) {
e.preventDefault();
//your logic for the button es here
}
//Everything else within the ancho will cause redirection***
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="foo" id="bar">OK
<span id="button">click me</span>
</a>
Try:
e.preventDefault();
Fiddle: http://jsfiddle/hfyy10a8/3/
You should use e.preventDefault();
on links:
$('#bar').on('click', function(e) {
e.preventDefault();
});
You can also add it on your example, all together:
$('#button').on('click', function(e) {
e.stopPropagation();
e.preventDefault();
});
use return false
:
$('#button').on('click', function (e) {
//some logic
$(this).css('color', 'red');
return false;
});
The reason why stopPropagation wont work is because it will prevent event handlers from running, not native behavior.
Try to use e.preventDefault()
instead:
$('#button').on('click', function (e) {
e.preventDefault();
});
See this explanation for why e.stopPropagation()
is not working.
I know this question already has an answer but however, I made mine work by returning false on the "onclick" attribute of the child element
.
<a href="https://google.">
<button onclick="return doTask()">Do not navigate</button>
</a>
function doTask(){
//... write action
return false; <- This prevents the click action from bubbling up to parent tag
}
本文标签: javascriptPrevent redirect when clicking on anchors child elementStack Overflow
版权声明:本文标题:javascript - Prevent redirect when clicking on anchors child element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740152545a2232931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论