[英]Trigger an href event only before the onclick
I found so many articles about "Trigger an href event only after the onclick" but my requirement is almost opposite ie "Trigger an href event only before the onclick". 我发现了很多有关“仅在onclick之后触发href事件”的文章,但我的要求几乎相反,即“仅在onclick之前触发href事件”。 I want my href execute first and then my onclick event.
我要先执行href,然后再执行onclick事件。
Here is my code: 这是我的代码:
<li id="menu-users" class="dropdown">
<a href="" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-users"></i>Manage Users<b class="caret"></b></a>
<ul class="dropdown-menu navmenu-nav">
<li><a href="manage_a" onclick="keepNavMenuOpen()">Manage A</a></li>
<li><a href="manage_b" onclick="keepNavMenuOpen()">Manage B</a></li>
<li><a href="manage_c" onclick="keepNavMenuOpen()">Manage C</a></li>
<li><a href="manage_d" onclick="keepNavMenuOpen()">Manage D</a></li>
</ul>
</li>
function keepNavMenuOpen() {
var element = document.getElementById("menu-users");
element.classList.add("open");
}
Have you tried a setTimeout call to delay the onclick event? 您是否尝试过setTimeout调用来延迟onclick事件?
Like this: 像这样:
<li><a href="manage_a" onClick="setTimeout(function(){keepNavMenuOpen.call(this)},20)">Manage A</a></li>
Refer to here: enter link description here 请参阅此处:在此处输入链接描述
You are misusing the anchor element in place of the button element. 您正在滥用锚元素代替按钮元素。 Change to
<button>
elements then attach your behaviors to it. 更改为
<button>
元素,然后将其行为附加到它。
Source: MDN's Page . 资料来源: MDN的Page 。
Anchor tags are often abused with the onclick event to create pseudo-buttons by setting href to "#" or "javascript:void(0)" to prevent the page from refreshing.
通过将href设置为“#”或“ javascript:void(0)”以防止页面刷新,锚点标记经常会与onclick事件一起使用以创建伪按钮。 These values cause unexpected behavior when copying/dragging links, opening links in a new tabs/windows, bookmarking, and when JavaScript is still downloading, errors out, or is disabled.
当复制/拖动链接,在新选项卡/窗口中打开链接,添加书签以及JavaScript仍在下载,错误输出或被禁用时,这些值会导致意外行为。 This also conveys incorrect semantics to assistive technologies (eg, screen readers).
这也将不正确的语义传达给辅助技术(例如屏幕阅读器)。 In these cases, it is recommended to use a instead.
在这种情况下,建议改用a。 In general you should only use an anchor for navigation using a proper URL.
通常,您只应使用锚来使用正确的URL进行导航。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.