javascript - 一个 div 位于另一个 div 中,两者都可点击。当按下内部按钮时,外部按钮也会被触发

标签 javascript jquery html

我想要实现的是这样的:我有一些 div(准确地说是一篇博客文章,比如 Reddit),我希望这个 div 完全可点击。它有很多数据,如作者、发布时间、内容、img等。但底部有一个保存按钮,允许用户保存以供以后使用。但是,当我单击“保存”按钮时,它还会触发帖子的 onlick,这会导致用户被重定向到帖子的详细信息页面。我不要那个。我只想触发保存按钮。情况图片:

Image of situation.

我所拥有的是:

<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 是 AJAX 函数,它将数据发送到 Django 服务器。

我该怎么做,仅触发保存按钮?

最佳答案

像这样重新排列你的 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>

关于javascript - 一个 div 位于另一个 div 中,两者都可点击。当按下内部按钮时,外部按钮也会被触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59113541/

相关文章:

javascript - 将对象字面量传递给 handlebars partial

javascript - 点击无法解除绑定(bind)的问题

html - 如何模拟指针事件 : none on an pseudo element in IE10?

jquery - Bootstrap 中的表单字段对齐困难

html - 如何获得宽度和高度完全相同的不同尺寸的图片墙?

html - 在响应式网格上设置 "same height"行部分的仅 CSS 解决方案

javascript - 推送通知的“showNotification”未定义

javascript - 转义/忽略 HAML 属性中的特殊字符

javascript - html+js转exe

javascript - jQuery 自动完成列表未显示