我想做的很简单。我想使整个部分都可以单击,除了跨度和下面的所有内容之外,因为我希望链接重定向到其他地方。
<section id="id" class="message">
<div class="message_body">
<font color="color">text</font>
<span style="style">
<a href="url">
<img src="linktoimage"></img>
</a>
</span>
<br></br>
<text name="message_comment" id="message_comment">message</text>
</div>
</section>
我尝试了以下方法:
$('.message:not(span)').click(function() {
window.location.href = "url";
});
和
$(".message").not("span").click(function() {
window.location.href = "url";
});
该行为在Chrome和Firefox中都是正确的。但是在Safari中,a href链接不起作用。当用html呈现时,它被视为链接,但是当您单击它时,将处理整个类的jQuery行为开始,就好像它忽略了“ not span”一样。无论如何,有没有重写它可以在Safari,Chrome和Firefox中使用,还是我做错了什么?
您可以定位跨度,以便他们单击时,单击事件将不会继续冒泡。
$('.message').on('click', function(){
console.log( 'yeah!' );
});
$('.message span').on('click', function (e) {
console.log( 'denied!' );
//stop the event from bubbling up out of the span to the parent elements
e.stopPropagation();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="message">
<span>Click Me!</span>
<div>Other Stuff</div>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句