简体   繁体   English

如何防止嵌套在另一个也使用onclick的元素上的链接上的click事件?

[英]How I can prevent the click event on a link which is nested on another element which also use onclick?

I have this simple code 我有这个简单的代码

<span class="doit"><a class="doit"href="ww.domain.com">text</a></span>

I want the click on the class "do it" fired a function, so I tried this 我想点击类“ do it”触发一个函数,所以我尝试了

$(document).on('click', '.doit', function(e) {

    // prevents to handle the click for nested objects again
    e.stopPropagation();

    alert("hello world"); 
});

Now I have the problem, that if I click on the <a href the link from href will open and not the on.click function will be fired. 现在,我<a href的问题是,如果单击<a href ,来自href的链接将打开,而不会触发on.click函数。

How I can prevent, that the link will be fired instead of the on.click event? 我如何防止触发链接而不是on.click事件?

UPDATE 1 更新1

I tried a suggested answer with this, failed because the href link is already opened. 我尝试了一个建议的答案,但失败,因为href链接已经打开。 I must prepare to open the link, only the on.click event should work. 我必须准备打开链接,只有on.click事件应该起作用。

<span class="doit">
 <a class="doit" href="http://www.example.com" target="_blank">webcms von 
  <span class="doit">ABC</span>
  <span class="doit">123</span>
 </a>
</span>
if ($(e.target).is("a")) {     
    e.preventDefault(); // stop the href 
    alert("I WILLL not go to " + e.target.href);
    e.cancelBubble = true; // do not click through
    e.stopPropagation(); // just stop the event or it will trigger again on the span
    return false; 
}

console.log('target:'+e.target);

In the console I read: [object HTMLSpanElement 在控制台中,我读到:[对象HTMLSpanElement

I must find a way, which works in every way the html-tags are ordered or nested. 我必须找到一种方法,该方法可以以各种方式对html标签进行排序或嵌套。

Update 2 更新2

if ($(e.target).is("a")) 
{     
      e.preventDefault(); // stop the href  
}
e.cancelBubble=true; // do not click through
e.stopPropagation(); // just stop the event or it will trigger again on the span
alert("hello world");

I see the alert 'hello world' but after that, the link will still be opened 我看到警报“ hello world”,但此后,该链接仍将打开

Just test 只是测试

However I do not recommend you give the same class to the two elements 但是,我不建议您对这两个元素使用相同的类

Here you can click the link and the onclick of the span triggers but not the href 在这里,您可以单击链接和范围触发器的onclick,但不能单击href

Nested spans are not correct HTML by the way 顺便说一下,嵌套范围不是正确的HTML

 $(document).on('click', '.doit', function(e) { if ($(e.target).is("a")) { e.preventDefault(); // stop the href alert("I WILLL not go to " + e.target.href); } e.cancelBubble=true; // do not click through e.stopPropagation(); // just stop the event or it will trigger again on the span alert("hello world"); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="doit"> <a class="doit" href="http://www.example.com" target="_blank">webcms von <span class="doit">4U</span> <span class="doit">.tools</span> </a> </span> 

Alternative 替代

 $(document) .on('click', 'a.doit', function(e) { e.preventDefault(); alert("I WILLL not go to " + e.target.href); }) .on('click', 'span.doit', function(e) { alert("hello world"); e.cancelBubble=true; e.stopPropagation() }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <span class="doit"> <a class="doit" href="http://www.example.com" target="_blank">webcms von <span class="doit">4U</span> <span class="doit">.tools</span> </a> </span> 

暂无
暂无

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

相关问题 我怎么知道哪个元素激活了javascript中的点击事件? - How can i know which element activated a click event in javascript? 如何取消具有“ onclick = xxx”元素的点击事件的绑定? - how to unbind click event for element which has “onclick=xxx”? 如何在触发onclick事件的函数中使用相同的元素 - How to use the same element into a function which triggers the onclick event 如何为我使用 jquery 到 append 一个 div,它还包括它的所有嵌套 div? - How can I use jquery to append a div for me which also includes all of it's nested divs as well? 我如何防止嵌套列表的单击事件 - how can i prevent nested list's click event 我如何单击另一个页面的链接,其中包含当前页面中的功能 - how can i click another page's link which includes functions in current page 如果选择包含相同嵌套元素的整个树,如何仅获取 DOM 元素的内容一次? - How can I grab the content of a DOM element only once if selecting an entire tree which also contains those same nested elements? 如何在html中调用元素的onClick事件并等待单击发生。来自javascript中的另一个函数 - how can I call an onClick event of an element in html and wait until the click occurs.From another function in javascript 如何模拟对元素的单击,这也会导致调用附加事件? - How to simulate a click on a element which also results in invoking attached events? 我如何知道Click事件上正在加载哪个脚本文件 - How can i know which script file is loading on Click event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM