Coffeescript:如何链接div,但如果点击子链接则覆盖链接 [英] Coffeescript: how to link div, but override link if child links are clicked

查看:183
本文介绍了Coffeescript:如何链接div,但如果点击子链接则覆盖链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得很难解释,所以如果你需要的话,请要求更多的信息。

I find it hard to explain, so please request more info if you need it.

我有这样的div:

<div id="clickable">
  <p>Some content</p>
  <a href="google.com" class="link">Google</a>
  <a href="yahoo.com">Yahoo</a>
</div>

我想要的div是可以点击的,并链接到一个属性的href类'link' 。这部分我做了:

I want the div to be clickable, and link to the href of an attribute with class 'link'. This part I have done:

  $('#clickable').on 'click', (ev) ->
    window.location = $(this).find('.link').attr('href')
    return false

但是,应该发生的是,如果用户单击div中的链接,则链接位置优先。所以,在上面的例子中,如果你点击yahoo它会直接到yahoo。现在,它强迫google。

But, what should happen is if a user clicks a link within the div, that links location takes precedence. So, in the above example, if you click yahoo it will direct to yahoo. At the moment, it forces google.

推荐答案

更新 window.location 并且随后的 return false (停止点击的传播)正在发生,而不考虑链接的正常行为。

The call to update window.location and subsequent return false (stopping the click's propagation) is happening regardless of the normal behaviour of a link.

尝试:

$('#clickable').on('click', (ev) ->
  unless $(ev.target).is('a')
    window.location = $('.link', @).attr('href') 
    false
)

这篇关于Coffeescript:如何链接div,但如果点击子链接则覆盖链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆