简体   繁体   English

一个 div 在另一个里面,都可以点击。 当按下内部的时,外部也被触发

[英]One div inside another, both clickable. When pressing the inner one, the outer is also triggered

what I would like to achieve is something like that: I have some div (a blog post to be precise, like Reddit) and I want this div to be whole clickable.我想要实现的是这样的:我有一些 div(准确地说是一篇博客文章,如 Reddit),我希望这个 div 是整个可点击的。 It has a lot of data, like the author, time posted, content, img, etc. But on the bottom is a save button which allows users to save it for later.它有很多数据,如作者、发布时间、内容、img 等。但底部是一个保存按钮,允许用户保存以备后用。 But when I click the save button it also triggers the post's onlick what causes that user is redirected to the detail page of the post.但是当我点击保存按钮时,它也会触发帖子的点击,导致用户被重定向到帖子的详细信息页面。 I do not want that.我不要那个。 I only want to trigger the save button.我只想触发保存按钮。 Image of the situation:情况图:

情况的图像。

What I do have is that:我所拥有的是:

<div onclick="location.href='/post/{{ post.id }}/';" style="cursor: pointer; ">
    <article class="media post">
        <div class="media-body">

            <!-- Whole body, user, date, content, img etc. -->

            <div>
                <a id="save-post-{{post.id}}" href="javascript:void(0);"
                    onclick="save_post('{{post.pk}}', '{{user.pk}}', this.id, '{{ csrf_token }}');">
                    Save
                </a>
            </div>
        </div>
    </article>
</div>

save-post is the AJAX function, that sends data to the Django server. save-post是 AJAX 函数,它将数据发送到 Django 服务器。

How do I do that, the save button is only triggered?我该怎么做,只触发保存按钮?

Just rearrange your htm a bit like this像这样重新排列你的 htm

<div>
<article class="media post">
    <div class="media-body"  onclick="location.href='/post/{{ post.id }}/';" style="cursor: pointer; ">

        <!-- Whole body, user, date, content, img etc. -->


    </div>
    <div>
            <a id="save-post-{{post.id}}" href="javascript:void(0);"
                onclick="save_post('{{post.pk}}', '{{user.pk}}', this.id, '{{ csrf_token }}');">
                Save
            </a>
        </div>
</article>

 </div>

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

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