如何防止 JavaScript 并发执行?
我有这样的 html 代码块,
<div id="some-div">
<a href="#" class="link-click">some link text</a>
<div id="small-text">here is the small text</div>
//another text block
</div>
<script type="text/javascript">
$('#some-div').click(function(){//some task goes here});
$('.link-click').click(function(){//some task goes here for link});
$('#small-text').click(function(){//some task goes here for small div});
</script>
当我们点击 div(id=some-div) 弹出消息时, 但是当我点击链接(class=link-click) 时,我需要运行另一个 javascript 函数。但问题是,当我单击链接时,它也会运行 some-div 函数。意味着单击后两个函数都会运行。
我如何防止并发 javascript 函数执行。我也在使用jquery。 谢谢
i have html code block like this
<div id="some-div">
<a href="#" class="link-click">some link text</a>
<div id="small-text">here is the small text</div>
//another text block
</div>
<script type="text/javascript">
$('#some-div').click(function(){//some task goes here});
$('.link-click').click(function(){//some task goes here for link});
$('#small-text').click(function(){//some task goes here for small div});
</script>
when we click on the div(id=some-div) popup message coming but when i click on the link(class=link-click) i need to run another javascript function. but the thing is when i click on link-click it also runs some-div function too. means on click both function gets run.
how do i prevent concurrent javascript function execution. i'm using jquery too.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 event.stopPropagation() 来避免事件在 DOM 树中冒泡。
You could use event.stopPropagation() to avoid having the event bubbling up the DOM tree.