简体   繁体   English

Javascript href和span标签是否锚定?

[英]Javascript href and span tag in anchor?

I have this markup, 我有这个标记,

<a href="http://www.example.com">Hiya<span class="delete">Delete</span></a>

And my js, 还有我的js

$('.delete').click(function() {
    window.location = 'http://www.example.com/example';
});

My problem the anchor tag always win presedence over my javascript window.location how can I overcome this? 我的问题是定位标记始终在我的javascript window.location占主导地位,我该如何克服呢?

Thanks 谢谢

You need to return false from the click() event to prevent the default behaviour: 您需要从click()事件返回false,以防止出现默认行为:

$('.delete').click(function(e) {
    e.stopPropagation();
    window.location = 'http://www.example.com/example';
    return false;
});

Use return false; 使用return false;

$('.delete').click(function() {
    window.location = 'http://www.example.com/example';
return false;
});

Have you tried with: 您是否尝试过:

$('.delete').click(function(ev) {
  ev.preventDefault()
  window.location = 'http://www.example.com/example';
});

Not sure, though, since "span" is not a clickable tag. 但不确定,因为“ span”不是可点击的标记。

You might be looking for http://api.jquery.com/event.stopPropagation/ 您可能正在寻找http://api.jquery.com/event.stopPropagation/

$(".delete").click(function(event){
  event.stopPropagation();
  // do something
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM