Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

javascript - 使用event.stopPropagation切换下拉菜单在第一次尝试时不会打开下拉菜单(Toggle dropdown with event.stopPropagation does not open dropdown at first try)

I want to override onclick method of dropdown, because I have to use event.stopPropagation(), because of the problem I've described here .

(我想覆盖下拉菜单的onclick方法,因为我在这里描述了这个问题,所以我必须使用event.stopPropagation()。)

I want to have a dropdown list next to save button, like Reddit does:

(我想在保存按钮旁边有一个下拉列表,就像Reddit那样:)

在此处输入图片说明

My HTML structure looks like this:

(我的HTML结构如下所示:)

<div class="dropdown">
    <a class="link-hover" data-toggle="dropdown" id="post{{post.pk}}" onclick="dropdown(event, this.id);">
        <i class="fa fa-chevron-down dark-icon" aria-hidden="true"></i>
    </a>
    <div class="dropdown-menu my-drop-menu">
        <a class="dropdown-item my-drop-menu-item" href="{% url 'post-update' post.pk %}">Edit</a>
        <!-- ... more a tags -->
    </div>
</div>

The JS function I ended up with is:

(我最终得到的JS函数是:)

function dropdown(event, postId) {
    event.stopPropagation();
    $('#' + postId).dropdown();
}

But the problem is that the dropdown does not open at first try.

(但是问题在于下拉菜单在第一次尝试时无法打开。)

Only on second click and subsequent ones.

(仅在第二次单击和后续单击时。)

How do I fix that?

(我该如何解决?)

Edit:

(编辑:)

On the other hand, when I .dropdown();

(另一方面,当我.dropdown();)

with .dropdown('toggle');

(用.dropdown('toggle');)

it opens only at first try.

(它仅在第一次尝试时打开。)

In the browser inspector the classes are not added at second try.

(在浏览器检查器中,不会在第二次尝试时添加类。)

  ask by northenofca translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...